I've made a simple program which is supposed to ask the user for the length of a set, fill it with numbers and find the minimal value of that set. When I run the code, program works fine until the last number of the set is entered. Console prompt keeps blinking but it doesn't react to the keyboard. The program stops at this point. I don't understand why it doesn't just stop asking for input. I'm using CodeBlocks 16.01 if that matters. Here is is the source code:
#include <iostream>
using namespace std;
int main()
{
int len;
cout << "How many elements?" << endl;
cin >> len;
int myset[len];
int temp;
cout << "Enter " << len <<" numbers: " << endl;
for (int x = 0; x < len; x++)
{
cin >> temp;
myset[x] = temp;
cout << endl;
}
int mini;
for (int i = 0; i < len; i++)
{
if (i = 0)
{
mini = myset[i];
}
else if(myset[i] < mini)
{
mini = myset[i];
}
}
cout << "Minimal value of this set: " << mini << endl;
}
You set i to 0 in the if(i = 0) line... I suppose you want to write "if (i==0)"
Related
I'm running into problems with my local debugger on visual studio while trying to learn C++, as soon as it gets to the end of the code it immediately closes and doesn't allow me to close it myself so I can't even see the final bit of output.
As far as I know there are no errors in my code and I tried the getchar() so that I could enter a character before the debugger closing.
Here's my code just incase there are errors but I don't think there are
#include <iostream>
using namespace std;
int search(char* pchs, int size, char key) {
int count = 0;
for (int i = 0; i < size; i++) {
if (pchs[i] == key)
count++;
}
return count;
}
int main() {
int size = 0;
char key = 0;
cout << "Please enter the size of the array" << endl;
cin >> size;
cout << "Please enter the key (a-z)" << endl;
cin >> key;
char* pchs = new char[size];
if (pchs != NULL) {
for (int i = 0; i < size; i++) {
pchs[i] = 97 + rand() % 26;
cout << pchs[i] << " ";
}
cout << endl;
cout << "No. Occurences: " << search(pchs, size, key) << endl;
}
delete[] pchs;
return 0;
}
Go to Tools -> Options and search for this option and disable it
You could try this method:
Right click on your project
Properties -> Configuration Properties -> Linker -> System -> Select Console (/SUBSYSTEM:CONSOLE)
So this is for a lab assignment and I already have it working, but one thing is bothering me. The assignment involves creating a 1-dimensional array and then manipulating it. I am supposed to allow a max of 100 inputs but the user does not have to use all 100. Right now, I am using a while statement to either break or allow another input to be entered. To break the statement, you have to enter a negative number (this is what I don't like and want to change). What other options are there to end the user input, once they are done entering their numbers? Is it possible to end the loop once you hit enter with nothing typed?
I have searched stackoverflow for the last 3 days and found some compelling stuff but could never get it to work.
Note, I get the void function is redundant here but that's besides the point (unless it actually affects my ability to achieve what I want).
Also, thanks in advance.
here is my code so far (my while statement is in the main)... be kind I'm a newbie to coding.
#include <iostream>
using namespace std;
void reverseElements(int array[], int size)
{
int tmp;
int j;
int i = size;
j = i - 1;
i = 0;
while (i < j)
{
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
i++;
j--;
}
cout << "I will now reverse the elements of the array." << endl;
cout << endl;
for (i = 0; i < size; i++)
{
cout << array[i] << " " << endl;
}
}
int main()
{
const int NUM_ELEMENTS = 100;
int iArr[NUM_ELEMENTS];
int i;
int myInput;
cout << "Enter your numbers, then enter a negative number to finish" << endl;
cout << endl;
for (i = 0; i < NUM_ELEMENTS; i++) //loop to obtain input
{
cin >> myInput;
if (myInput < 0) //checks for negative number to end loop
{
break;
}
else //continues to allow input
{
iArr[i] = myInput;
}
}
cout << endl;
reverseElements(iArr, i);
return 0;
}
Probably the easiest solution: let your user choose how many numbers to write before actually writing them.
int readNumbersCount()
{
int const numbersMin = 1;
int const numbersMax = 100;
int numbersCount = -1;
while (numbersCount < numbersMin || numbersCount > numbersMax)
{
std::cout <<
"How many numbers are you going to enter? Choose from " <<
numbersMin << " to " << numbersMax << ":\n";
std::cin >> numbersCount;
}
return numbersCount;
}
int main()
{
int const numbersCount = readNumbersCount();
for (int i = 0; i < numbersCount; ++i)
{
// read the numbers etc.
}
return 0;
}
I wrote readNumbersCount() as a separate function to extract numbersMin and other "one-use" identifiers from main() and to make main()'s numbersCount const.
I have edited the main function a little bit.
Here the user is asked how many elements he wants to enter .
and doing the memory allocation dynamically so as to save space
int main()
{ int n=101;
while(n>100){
cout<<"How many numbers do you want to enter";
cin>>n;
}
int *ptr=new(nothrow)int[n];
for (int i=0;i<n;i++){
cout << "Enter your number" << endl;
cin>>ptr[i];
}
cout << endl;
reverseElements(ptr, n);
return 0;
}
I am messing around with dynamic arrays for a user defined amount of inputs for an and gate.
The issue I am running into is that I don't know how many inputs the user is going to test and I need to be able to have an if-else statement that tests each input.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class logic_gate {
public:
int x = 0;
};
int main() {
int userInput = 0;
cout << "How many inputs do you want on your and gate?: ";
cin >> userInput;
cout << endl;
logic_gate *and_gate = new logic_gate[userInput];
cout << endl << "Please enter the values of each bit below . . ." << endl <<
endl;
int userTest1 = 0;
for (int i = 0; i < userInput; i++) {
cout << "#" << i + 1 << ": ";
cin >> userTest1;
and_gate[i].x = userTest1;
}
return 0;
}
Here is the code that I am currently trying to find a solution for.
To implement an AND gate with n inputs you can simply do:
int output = 1;
for (int i = 0; i < n; ++i)
{
if (!and_gate [i])
{
output = 0;
break;
}
}
// ...
Use Vector data structure, you don't need to tell its size while declaring, unlike array, and it can grow automatically.
To read input till it's arriving, put cin inside while loop condition. I used getline to read whole line and work with it, so that whenever user presses enter button at empty line, program will think that no more input is coming anymore, and will start calculating 'And' of inputs.
//don't forget to import vector
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class logic_gate {
public:
int x = 0;
logic_gate(){ //default constructor
}
logic_gate(int k){ //another constructor needed
x = k;
}
};
int main(){
cout << endl << "Please enter the values of each bit below . . ." << endl;
vector<logic_gate> and_gate; //no need to tell size while declaration
string b;
while(getline(cin, b)){ //read whole line from standard input
if (b == "\0") //input is NULL
break;
and_gate.push_back(logic_gate(stoi(b))); //to convert string to integer
}
if (!and_gate.empty()){
int output = and_gate[0].x;
for (int i = 1; i < and_gate.size(); i++){
output = output & and_gate[i].x;
}
cout << "And of inputs is: " << output << endl;
}
else{
cout << "No input was given!\n";
}
return 0;
}
Feel free to ask if some doubts linger
I figured out what I wanted to do. Thanks to everyone who helped and especially Paul Sanders. Below is my final code.
#include <iostream>
using namespace std;
class logic_gate {
public:
int x = 0;
};
int main() {
int userInput;
int output = 1;
cout << "How many inputs do you want on your and gate?: ";
cin >> userInput;
cout << endl;
logic_gate *and_gate = new logic_gate[userInput];
cout << endl << "Please enter the values of each bit below . . ." << endl <<
endl;
int userTest1;
for (int i = 0; i < userInput; i++) {
cout << "#" << i + 1 << ": ";
cin >> userTest1;
and_gate[i].x = userTest1;
}
if (userInput == 1) {
output = userTest1;
cout << "The test of " << userTest1 << " is " << output << endl << endl;
}
else if (userInput > 1) {
for (int i = 0; i < userInput; i++) {
if (!and_gate[i].x)
{
output = 0;
break;
}
}
cout << "The test of ";
for (int i = 0; i < userInput; i++) {
cout << and_gate[i].x;
}
cout << " is " << output << endl << endl;
}
return 0;
}
#include <iostream>
#include <math.h>
using namespace std;
int main() {
int arrowBaseHeight = 0;
int arrowBaseWidth = 0;
int arrowHeadWidth = 0;
int i = 0;
int j = 0;
cout << "Enter arrow base height:" << endl;
cin >> arrowBaseHeight;
cout << "Enter arrow base width:" << endl;
cin >> arrowBaseWidth;
cout << "Enter arrow head width:" << endl;
cin >> arrowHeadWidth;
cout << endl;
// Draw arrow base
while (i <= arrowBaseHeight){
while (j <= arrowBaseWidth){
cout << "*";
j++;
}
cout << endl;
j = 0;
i++;
}
// Draw arrow head (width = 4)
return 0;
}
I am trying to write a simple program that takes 3 user entered integers and assigns them to arrowBaseHeight, arrowBaseWidth, and arrowHeadWidth. The output should be a series of asterisks (*) that print out like:
**
**
**
****
***
**
*
to create an image of an arrow.
I have been trying to figure out the best way to print out the base portion of the arrow using nested loops (I have been using while but if for is better, let me know). I have tried multiple different ways and I have yet to figure one out that doesn't throw back an error. I have yet to get to the arrow head portion but if anyone wants to point me in the right direction, it would be helpful!
You were close, but if you want for a loop to be executed exactly n times, starting your counter i at 0, the condition should be i < n, not i <= n.
About the head, you just have to decrement the number of characters printed in every line, starting from the inputted width.
#include <iostream>
int main()
{
using std::cout;
using std::cin;
int arrowBaseHeight = 0;
cout << "Enter arrow base height:\n";
cin >> arrowBaseHeight;
int arrowBaseWidth = 0;
cout << "Enter arrow base width:\n";
cin >> arrowBaseWidth;
int arrowHeadWidth = 0;
cout << "Enter arrow head width:\n";
cin >> arrowHeadWidth;
cout << '\n';
// Draw arrow base
for ( int i = 0; i < arrowBaseHeight; ++i )
{
for ( int j = 0; j < arrowBaseWidth; ++j )
{
cout << '*';
}
cout << '\n';
}
// Draw arrow head
for ( int i = 0, width = arrowHeadWidth; i < arrowHeadWidth; ++i, --width )
{
for ( int j = 0; j < width; ++j )
{
cout << '*';
}
cout << '\n';
}
return 0;
}
You can see a lot of repeated code, consider refactoring it using some custom functions, instead.
You should change the condition of the while loops to:
while (i < arrowBaseHeight) and
while (j < arrowBaseWidth).
And for the arrowHeadWidth you could try to get the middle of the arrowBaseHeight. Maybe like this
int r = 0;
if(i == arrowBaseHeight / 2)
{
while(r < arrowHeadWidth)
{
cout << "*";
r++;
}
}
I haven't tested it. I hope it helps.
All you need to do is to add a new variable which could indicate what are you need to print right now.
The rule is :
If: up to half of "arrowBaseHeight" iteration you need to print the base
Else: print the head and after that decrease in 1
In addition finger rule - if you are using "while" and you need to increase an iterator it always indicate that you need to use For
#include <iostream>
#include <math.h>
using namespace std;
int main() {
int arrowBaseHeight = 0;
int arrowBaseWidth = 0;
int arrowHeadWidth = 0;
int newArrowBaseWidth=0;
cout << "Enter arrow base height:" << endl;
cin >> arrowBaseHeight;
cout << "Enter arrow base width:" << endl;
cin >> arrowBaseWidth;
cout << "Enter arrow head width:" << endl;
cin >> arrowHeadWidth;
cout << endl;
// Draw arrow base
for(int i=0; i < arrowBaseHeight; i++){
newArrowBaseWidth= i < arrowBaseHeight/2 ? arrowBaseWidth : arrowHeadWidth--;
for(int j=0; j < newArrowBaseWidth; j++){
cout << "*";
}
cout << endl;
}
// Draw arrow head (width = 4)
return 0;
}
Another thing is if you want to iterate n time you need to change the condition from =< that here mean - n+1 time, to <
This is my first program on C++. I successfully build it. When I run it, Windows keep giving me program is stop working, the same result that I try to run it with eclipse.
Here is my code:
#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <vector>
using namespace std;
int main(){
string input;
vector<double> value;
int count = 0;
while(input != "#") {
cout << "Enter value " << count + 1 << "\n";
cin >> input;
cout << input;
if (input != "#") {
value[count] = atof(input.c_str());
}
count++;
}
cout << count;
double sum = 0;
for (int i = 0; i < count; i++) {
sum += value[i];
}
double ave = sum/count;
double dev = 0;
for (int i = 0; i < count; i++) {
dev += pow((value[i] - ave), 2);
}
dev = sqrt(dev / (count - 1));
cout << "\nThe average is " << ave << "\n";
cout << "The standard deviation is" << dev << "\n";
return 0;
}
Anyone has any idea? Thank you.
value[count] = atof(input.c_str());
is a problem since value does not have enough space in it. Use
value.push_back(atof(input.c_str()));
instead.
You also have a logic error in the while loop. count will be incremented even when the input is "#". I recommend changing it to:
while(true) {
cout << "Enter value " << count + 1 << "\n";
cin >> input;
cout << input;
if (input == "#") {
break;
}
value.push_back(atof(input.c_str()));
}
count = value.size();
I tried the code on other's computer. It works great. I think something goes wrong for my compiler.