C++ program repeating when it is supposed to terminate [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have been trying to make a C++ "math tutor" program. It's purpose is to ask the user if they want to do addition, subtraction, or multiplication, and once they make their selection and the calculation is complete, they are prompted to type in 0 to terminate the program and 1 to repeat it.
I tried using a do-while loop for this, but when I ran it, I found that as long as the user pressed 0 or 1, the program would repeat and it would never terminate.
Here's the code (without the math part as that really isn't relevant):
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
/*
* program to serve as math tutor for a young student
*/
int main(int argc, char** argv) {
int repeatChoice = 0;
//actual program has more variables here, but those are all math variables.
do {
//in this area was just code for the actual math part
cout << "Do you want to repeat this program (0 for no, 1 for yes)?" << endl;
cin >> repeatChoice;
while (repeatChoice != 0 && repeatChoice != 1) {
cout << "Invalid input. Please select 0 or 1." << endl;
cin >> repeatChoice;
}
if (repeatChoice = 0)
break;
} while (repeatChoice = 1);
return 0;
}
If anyone thinks seeing the full code will help, I'll edit the question to add it.
Please note that I'm new to programming and haven't researched any complex concepts really, so please if you can, explain it in relatively simple terms.

From what I can see the problem is in this part of the code:
...
if (repeatChoice = 0)
break;
} while (repeatChoice = 1);
...
You are using the assignment operator = instead of the comparison operator ==.

Related

while exercise with return in c++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I've just started coding in c++ and now I have an exercise that I can't do because the code seems to not work.
I've to find the max and the min with a sequence of n numbers (in this case i already know that they are 4). I've to use while.
I've just started so I don't know how return properly works...
there aren't syntactical errors but when I run it ask me the number but then it says that the algorithm ends with 0 value.
Here's the code, if you can help me thank you!
#include <iostream>
using namespace std;
main ()
{ float mag,min,i,a;
mag=0;
min=0;
i=0;
while (1)
{
if (i<5)
{ cout<<"insert a number"<<endl;
cin>>a;
if (i = 0)
{ mag=a;
min=a;
}
else
{ if (a<min)
{ min=a;
}
else
{ if (a>mag)
{ mag=a;
}
}
}
i=i+1;
}
else
{ cout<<"maggiore= "<<mag<<endl<<"min= "<<min<<endl;
}
return 0;
}
system ("pause");
}
I see at minimum one problem:
if (i = 0)
This is assignment of i to 0 and compare the result of assignment, which is always false if you assign a 0.
I believe you want only compare and not assign, so you have to use:
if ( i == 0 )
The next problem is
return 0;
This will always end the current function, if the function is main(), it will terminate your program. In your case, you can simply remove the return statement, as in main it will return 0 by default if the function ends.
But if you use
while (1)
without any return, your program runs endless. I don't know what is the expected behavior.
Rest looks fine.
Hint: Your indentation is a bit special. :-)
1st it should be i==0 not i=0 in the if
2nd you should place that return 0 after that cout maggiore or it will close after the first loop
3rd you don't need that system pause there. It does literally nothing. You should either remove it or place it exactly before the return.

compiler issues with simple array program that wont build due to limitations in scope perhaps [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
this is my first question on stack overflow.. Im having compiler issues and wanted to figure out if anyone could help me find out why.
this is the unfortunate error message I recieve every time I attempt to compile.
//*************************************************************************
// This program uses array with a for loop control structure to prompt the user to enter their most convenient days of the week to work
#include <iostream>
using namespace std;
#include <array>
#include <cstddef>
int main() {
int whichDay = 0;
array< string, 7> daysOfweek =
{
"sunday",
"monday",
"tuesday",
"wednesday",
"thursday",
"Friday",
"saturday"
};
for (size_t x= 0; x< daysOfweek.size(); x++)
{
cout << daysOfWeek[x] << endl;
};
cout << "enter your work day: " ;
cin >> whichDay;
cout << daysOfWeek[whichDay] += 1
return 0;
}
This is your issue: cout << daysOfWeek[whichDay] += 1.
What would you expect the result of "monday" += 1 to be?
You probably just want to display the chosen day, so you just need:
cout << daysOfWeek[whichDay];
Well, you also may want to check that the input value is in the valid range :)

Conditional statement evaluating wrong [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
According to the program below, if donuts = 1 it will assign 0 to donuts, but if I run this it shows that 0 is assigned to donuts. Please tell me what I am over looking. Because how I see this code donuts should equal 12, because donuts should have fallen into the else:
#include <iostream> // Header file to enable console I/O
#include <string> // Header file to enable string
#include <iomanip> // enables maniplution of io strem
using namespace std;
// Begin main Function Definition
int main()
{
int donuts = 10;
if (donuts = 1)
{
donuts = 0;
}
else
{
donuts += 2;
}
cout<< donuts;
system("PAUSE");
return 0;
}
// End of main function
if (donuts = 1)
should be:
if (donuts == 1)
//^^^
You need to use logical equal == or assignment =

C++ ? Why the value does not change ? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I just learned how to use this operator : ->,
and I am trying to create practice programs so I can remember it and be familiar with it.
I created a program that inputs my health and then heal my (add health) using the -> operator.
But when I run program, my health stays at 50 (cause I set my current health to 50).
here is my code :
#include <iostream>
using namespace std;
struct myhealth
{
unsigned short my_health;
};
void addhealth(myhealth* addhealth)
{
addhealth->my_health += 50;
};
int main()
{
myhealth player;
player.my_health = 50;
cout << "My earlier health : " << player.my_health << endl;
myhealth();
cout << "My current health : " << player.my_health;
cin.get()
return 0;
}
You never call addhealth and so the value is never modified.
This line of code:
myhealth();
appears to have been written in error. Instead I think you meant to write:
addhealth(&player);

how to get correct answer merge 2 sorted arrays?! C++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
i wrote a little algorithm for marge to sorted array. but i have problem with that.
#include <iostream>
using namespace std;
int main() {
// main function started form here:
int firstArray[10] = {1,3,5,7,9,11,13,15,17,19};
int secondtArray[10] = {2,4,6,8,10,12,14,16,18,20};
int mergedArray[20];
int firstCounter=0 , secondtCounter=0 , mergedCounter=0;
while(firstCounter < 10 && secondtCounter < 10){
if(firstArray[firstCounter] < secondtArray[secondtCounter]){
mergedArray[mergedCounter] = firstArray[firstCounter];
firstCounter++;
} else {
mergedArray[mergedCounter] = secondtArray[secondtCounter];
secondtCounter++;
}
mergedCounter++;
}
while(firstCounter < 10) {
mergedArray[mergedCounter] = firstArray[firstCounter];
firstCounter++;
mergedCounter++;
}
while(secondtCounter < 10) {
mergedArray[mergedCounter];
secondtCounter++;
mergedCounter++;
}
for(int j=0; j<20; j++){
//cout << mergedArray[j] << endl;
}
cout << mergedArray[19];
return 0;
}
in outpout for array mergedArray[19] i get something like this: 2686916!!!
i don't know why i get this value. how can i fix that. and why i get this value.
Typo in last while. You may increase your warning level to let your compiler show you your typo (warning: statement has no effect [-Wunused-value]).
while(secondtCounter < 10) {
mergedArray[mergedCounter];
secondtCounter++;
mergedCounter++;
}
should be
while(secondtCounter < 10) {
mergedArray[mergedCounter] = secondtArray[secondtCounter];
secondtCounter++;
mergedCounter++;
}
As pointed out by WhozCraig's comment, you're not assigning any value to mergedArray[19] because you left out the assignment part of the statement.
Since you haven't assigned a value, it's printing out whatever value happens to be at that memory address from previous usage. If you run your program (as it's currently written) several times, you'll see that the number there might change. Also, if you'd printed out the values in mergedArray before assigning anything, you'd see more such meaningless (to you in the current application) numbers.