-
Whats wrong with this..
#include <stdio.h>
#include <iostream.h>
int main()
{
int first_number = 0;
int second_number = 0;
int answer = first_number+=second_number;
int make_one_letter_and_enter_to_exit;
do
{
cout<<"Please enter the first number\n";
cin>>first_number;
cout<<"Please enter the second number\n";
cin>>second_number;
cout<<"Answer is: "<<answer;<<"\n";
cin>>answer;
cout<<"Type anything and press enter to exit";
cin>>make_one_letter_and_enter_to_exit;
return 0;
}
}
-
please use code tags....
Code:
#include <stdio.h>
#include <iostream.h>
int main()
{
int first_number = 0;
int second_number = 0;
int answer = first_number+=second_number;
int make_one_letter_and_enter_to_exit;
do
{
cout<<"Please enter the first number\n";
cin>>first_number;
cout<<"Please enter the second number\n";
cin>>second_number;
/*suggestion:
answer = first_number + second_number;
*/
cout<<"Answer is: "<<answer<<endl; //note: answer was defined as 0 from start
cin>>answer; //why?
cout<<"Type anything and press enter to exit";
cin>>make_one_letter_and_enter_to_exit;
return 0;
} while(1);
return 0;
}
First, the do command is used in association with a do-while loop and you must have a loop/loop condition. Hence the while(1);
Second, why would you use a do-while loop when its going to return before it even loops? In any case just include the while at the end of the do's scope, and remember the ';'
-LC
-
-
Code:
#include <stdio.h>
#include <iostream.h>
int main()
{
int first_number = 0;
int second_number = 0;
int answer = 0; //adding 1st and 2nd number here has no effect... since they are both zero.
int make_one_letter_and_enter_to_exit;
do
{
cout<<"Please enter the first number\n";
cin>>first_number;
cout<<"Please enter the second number\n";
cin>>second_number;
/*suggestion:
answer = first_number + second_number;
*/
cout<<"Answer is: "<<answer<<endl; //note: answer was defined as 0 from start
cin>>answer; //why?
cout<<"Type anything and press enter to exit";
cin>>make_one_letter_and_enter_to_exit;
return 0;
} while(1);
return 0;
}
-
What about this?
#include <stdio.h>
#include <iostream.h>
int main()
{
int first_number = 0;
int sign;
int second_number = 0;
int answer; // answer
int make_one_letter_and_enter_to_exit; // to make the program stay up afterthe answer is sent
do
{
cout<<"Welcome to my calculator program\n\n"; // welcome message
cout<<"first number:\n"; // here u write the first number
cin>>first_number; // saves the first number
cout<<"Calculate function(+,-,*, or /):\n"; // here you chooce what you gonna calculate the 2 numbers with
cin>>sign;
cout<<"second number:\n"; // here u write the second number
cin>>second_number; // saves the second number
cout<<"Answer is: "<<first_number sign second_number<<endl; // This is where the problem is.. what I shall use instead of "sign" only
cin>>answer;
cout<<"Type anything and press enter to exit"; // to make the prog stay up after the answer is displayed, but it dont
cin>>make_one_letter_and_enter_to_exit;
return 0;
} while(1);
return 0;
}
-
PLEASE use code tags.
do a SWITCH sentence...
Code:
switch(sign) {
case '*':
answer = first_number * second_number;
break;
case '+':
answer = first_number + second_number;
break;
//etc...
}
then just cout the answer.
-
error by using that?
aha.. Ok sorry, I will in from now on! First time using this forum :)
When I added this, it just didnt ask me the for the second_number, but it stopped when it came to the "which sign" question. That is because of the "brake;" or ?
Also I am very new with c++ so dont laugh if this seems very stupid to you :) hehe
PHP Code:
#include <stdio.h>
#include <iostream.h>
int main()
{
int first_number = 0;
int sign;
int second_number = 0;
int answer; // answer
int make_one_letter_and_enter_to_exit; // to make the program stay up afterthe answer is sent
do
{
cout<<"Welcome to my calculator program\n\n";
cout<<"first number:\n"; // here u write the first number
cin>>first_number; // saves the first number
cout<<"Calculate function(+,-,*, or /):\n";
cin>>sign;
switch(sign) {
case '*':
answer = first_number * second_number;
break;
case '+':
answer = first_number + second_number;
break;
case '-':
answer = first_number - second_number;
break;
case '/':
answer = first_number / second_number;
break;
}
cout<<"second number:\n";
cin>>second_number;
cout<<"Answer is: "<<answer<<endl;
cin>>answer;
cout<<"Type anything and press enter to exit";
cin>>make_one_letter_and_enter_to_exit;
return 0;
} while(1);
return 0;
}
-
it doesnt work because sign is declared as an int, yet you want a char. keep everything the same but replace withand you will be good to go
but why do you still have the at the end?
-
Change
to
You should move
Code:
cout<<"second number:\n";
cin>>second_number;
before the swith-case otherwise you'll end up with the wrong answer.
Dont understand why you have that second line there. You showing the user the answer but then you want the user to input some other data and place it into the answer variable. Dont think you need that second line.
Code:
cout<<"Answer is: "<<answer<<endl;
cin>>answer;
Your code looks something like this...
Code:
do
{
// your code..
cout<<"Type anything and press enter to exit";
cin>>make_one_letter_and_enter_to_exit;
return 0;
} while(1);
return 0;
A suggestion would be,
Code:
char quit;
do
{
cout << "Do you want to quit (Y/N) ? ";
cin >> quit;
}while ( quit == 'N' || quit == 'n');
return 0;