Reputation: 11
The issue I am running into is that when I run the code, it adds correctly but when it comes time to run the if
or else
statement, nothing happens. It always just ends.
#include <iostream>
using namespace std;
int main()
{
int firstNumber;
int secondNumber;
int sum;
char restart = 1;
while (restart == 1)
{
cout<<"Welcome to My Calculator!\n\n";
cout<<"What is the first number you would like to add?\n\n";
cin>>firstNumber;
cout<<"What is the second number you would like to add?\n\n";
cin>>secondNumber;
cout<<"Wonderful! Getting together your result now....\n\n";
sum = firstNumber + secondNumber;
cout<< sum<<endl;
cout<<"If you have another question, just enter 1, if not press 2!\n\n";
cin>>restart;
if (restart == 1)
{
cout<<"No problem!\n\n";
}
else
{
cout<<"Goodbye!";
}
}
return 0;
}
Upvotes: 0
Views: 647
Reputation: 1453
char restart = 1;
if (restart == 1)
needs to be
char restart = '1';
if(restart == '1')
There is a difference between 1 and '1'. When you want to compare char
s you use the ''
marks.
Also, you should also always initialize your int
s
This is because C++ doesn't automatically set it to zero for you. So, you should initialize it yourself:
int sum = 0;
An uninitialized variable has a random number such as 654654,-5454, etc. (If it doesn't invoke undefined behavior when reading it)
Upvotes: 4