Dec 8, 2014 at 11:11pm UTC
Im a new coder when it comes to loops. i have worked with GML but c++ is more unforgiving. I am trying to get a loop that goes back to the beggining after user input of y for yes or n for no.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include <iostream>
using namespace std;
/*
Name: Money conversion
Author:Zachary Horton
Date: 12/2/14 13:16
Description: Convert US
dollars, to Pesos or
Canadian dollars
*/
int main()
{
start:
int U;
cout << "Please enter US dollar amount: $" ;
cin >> U;
double P = 14.11;
double C = 1.14;
(P *= U);
(C *= U);
cout << "That will get you: " << endl;
cout << "" << endl;
cout << "MXN: " << P << endl;
cout << "" << endl;
cout << "CAD: " << C << endl;
cout << "" << endl;
//here lies the problem
char A;
cout << "New amount?" ;
cin >> A;
system ("PAUSE" );
return 0;
}
Last edited on Dec 8, 2014 at 11:22pm UTC
Dec 8, 2014 at 11:20pm UTC
i assume you mean put char instead of int?
Dec 8, 2014 at 11:21pm UTC
Correct, and i advise you to move all your variable declarations out of the loop.
Last edited on Dec 8, 2014 at 11:22pm UTC
Dec 8, 2014 at 11:23pm UTC
i need the syntax for a loop
i was trying IF/Else but couldnt get it to work
Dec 8, 2014 at 11:27pm UTC
is this correct?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
int main()
do {
{
start:
int U;
cout << "Please enter US dollar amount: $" ;
cin >> U;
double P = 14.11;
double C = 1.14;
(P *= U);
(C *= U);
cout << "That will get you: " << endl;
cout << "" << endl;
cout << "MXN: " << P << endl;
cout << "" << endl;
cout << "CAD: " << C << endl;
cout << "" << endl;
//here lies the problem
char A;
cout << "New amount?" ;
cin >> A;
}while (A != 'y' );
Last edited on Dec 8, 2014 at 11:29pm UTC
Dec 8, 2014 at 11:29pm UTC
Almost, your A variable is out of scope, move it out of the loop, and while you're at it move the other variable declarations out as well.
Dec 8, 2014 at 11:38pm UTC
You need a open curly brace right after int main()
either on line 1 or 2 or atleast before any code starts.
Move line 11 to line 2.
And you need a closing curly brace at the end of the program to end the main function as well.
Last edited on Dec 8, 2014 at 11:40pm UTC
Dec 8, 2014 at 11:41pm UTC
This is what it should look like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include <iostream>
using namespace std;
int main()
{
double P = 14.11;
double C = 1.14;
int U;
char A;
do {
cout << "Please enter US dollar amount: $" ;
cin >> U;
(P *= U);
(C *= U);
cout << "That will get you: " << endl;
cout << "" << endl;
cout << "MXN: " << P << endl;
cout << "" << endl;
cout << "CAD: " << C << endl;
cout << "" << endl;
cout << "New amount?" ;
cin >> A;
}while (A != 'n' );
}
means run the loop as long as A is not equal to(!=) 'n'.
So as long as variable A is not equal to n the program will run
Last edited on Dec 8, 2014 at 11:42pm UTC
Dec 8, 2014 at 11:41pm UTC
does it not need return 0 or system pause?
Last edited on Dec 8, 2014 at 11:44pm UTC
Dec 8, 2014 at 11:46pm UTC
thanks for the help solved majority problem. was confused on "Return 0" and "System ("PAUSE")' because thats how i was trained.
Dec 8, 2014 at 11:50pm UTC
It does actually need it, it's good practise to use it everytime, i just forgot.
Dec 8, 2014 at 11:52pm UTC
so after while? or include in the loop?
Dec 8, 2014 at 11:55pm UTC
At the bottom of the program on the line before the terminating close curly brace.
Move Line 43 to 44 and put return 0;
on line 43.
That is how i would do it.
Dec 8, 2014 at 11:59pm UTC
Thank you again. I usually am good with this stuff and enjoy coding. i like to have conformation i am doing stuff right and you helped me on that so thank you for the third time.