Using loops

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
I am trying to get a loop that goes back to the beggining after user input of y for yes or n for no.


I assume it is here:
1
2
3
4
5
int A;
   
cout << "New amount?";
   
cin >> A;


First of all, you declared a variable of type int and expect a y or n? If input is wrong program will become defect if you can say it like that.
i assume you mean put char instead of int?
Correct, and i advise you to move all your variable declarations out of the loop.
Last edited on
i need the syntax for a loop

i was trying IF/Else but couldnt get it to work
This is an example of a do-while loop.

1
2
3
do {
// Code goes here
}while(A != 'n');
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
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.
Variables are out of loop. im assuming because it would keep re-declaring them and overload the buffer. I just need to figure out how to make it return to start on yes and terminate on n.



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()

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 != 'y'); 
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
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'); 
}


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
does it not need return 0 or system pause?
Last edited on
thanks for the help solved majority problem. was confused on "Return 0" and "System ("PAUSE")' because thats how i was trained.
It does actually need it, it's good practise to use it everytime, i just forgot.
so after while? or include in the loop?
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.
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.
No problem
Topic archived. No new replies allowed.