Infinite loop

Hi I need help with my program. My teacher wants me and my friend to fix our program so that when the user chooses to do addition that they can enter an infinite amount of numbers to add, he also wants us to do that with the subtraction, and division.
Here's the program:

#include <iostream>
using namespace std;
int main()
{{
bool cont = true;
char ans;
cout << "Hi\n";
int x;
float a, b, c;
do
{
cout<< "Would you like to use this program? Y or N?" << endl;
cin>> ans;
if ((ans == 'y' || ans == 'Y')) cont=true;
else
if ((ans == 'n' || ans == 'N')) cont=false;
else cout<< "Please enter either y or n.\n";
}
while ((ans!='y') && (ans!='Y') && (ans!='n')&& (ans!='N'));
if ( ans == 'n' || ans == 'N')
{ cout << "\nGoodbye "<< endl;
}
while(cont) {
if ( ans == 'y')
cout << " For subtraction, please press 1 " << endl;
cout << " For addition, please press 2 " << endl;
cout << " For division between two numbers, please press 3 " << endl;
cout << " For multiplication, please press 4 " << endl;
cout << " For modulus between two numbers, please press 5 " << endl;
cin>> x;
if (x==1)
{
cout<<"\nYou chose to subtract between two numbers.\n";
cout<<"Please enter first number: ";
cin>>a;
cout<<"\nHow much do you want to subtract from first number?: ";
cin>>b;
c=a-b;
cout<<"\nThis is your answer: "<<c<<"\n"<<"Thank you for using this program!\n\n";}
else if(x==2)
{
cout<<"\nYou chose to add two numbers together.\n";
cout<<"Enter first number: ";
cin>>a;
cout<<"\nHow much do you want to add to first number?: ";
cin>>b;
c=a+b;
cout<<"\nThis is your answer: "<<c<<"\n"<<"Thank you for using this program!\n\n";}
else if (x==3)
{
cout<<"\nYou chose to divide two numbers\n";
cout<<"Enter first number: ";
cin>>a;
cout<<"\nHow much do you want to divide the first number by?: ";
cin>>b;
c=a/b;
cout<<"\nThis is your answer: "<<c<<"\n"<<"Thank you for using this program!\n\n";}
else if (x==4)
{
cout<<"\nYou chose to multiply two numbers\n";
cout<<"Enter first number: ";
cin>>a;
cout<<"\nHow much do you want to multiply it by?: ";
cin>>b;
c=a*b;
cout<<"\nThis is your answer: "<<c<<"\n"<<"Thank you for using this program!\n\n";}
else if (x==5)
{
int d, e;
cout<<"\nYou chose to get the modulus of two numbers\n";
cout<<"Enter first number: ";
cin>>d;
cout<<"\nHow much do you want to divide the first number by?: ";
cin>>e;
int f=d%e;
cout<<"\nThis is your remainder: "<<f<<"\n"<<"Thank you for using this program!\n\n";}
cout <<"Do you want to continue using this program, enter y or n:" ;
char contletter;
cin >> contletter;
if (contletter == 'y' || contletter == 'Y')
cont = true;
else cont = false;
if ( ans == 'n')
{ cout << "Goodbye "<< endl;
}

} // end else
} // end while
} // end main
Last edited on
ShellZ,

For starters I'll make the code easier for us to read. I'll take a look at it, just give me a few minutes...

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
using namespace std;
int main()
{ //I believe the second { here was a typo
bool cont = true;
char ans;
cout << "Hi\n";
int x;
float a, b, c;

do
{
     cout<< "Would you like to use this program? Y or N?" << endl;
     cin>> ans;
     if ((ans == 'y' || ans == 'Y')) cont=true;
     else if ((ans == 'n' || ans == 'N')) cont=false;
     else cout<< "Please enter either y or n.\n";
}
while ((ans!='y') && (ans!='Y') && (ans!='n')&& (ans!='N'));

if ( ans == 'n' || ans == 'N')
{ 
     cout << "\nGoodbye "<< endl;
}

while(cont) 
{
     if ( ans == 'y')
     cout << " For subtraction, please press 1 " << endl;
     cout << " For addition, please press 2 " << endl;
     cout << " For division between two numbers, please press 3 " << endl;
     cout << " For multiplication, please press 4 " << endl;
     cout << " For modulus between two numbers, please press 5 " << endl;
     cin>> x;
     if (x==1)
     {
          cout<<"\nYou chose to subtract between two numbers.\n";
          cout<<"Please enter first number: ";
          cin>>a;
          cout<<"\nHow much do you want to subtract from first number?: ";
          cin>>b;
          c=a-b;
          cout<<"\nThis is your answer: "<<c<<"\n"<<"Thank you for using this program!\n\n";
     }
     else if(x==2)
     {
          cout<<"\nYou chose to add two numbers together.\n";
          cout<<"Enter first number: ";
          cin>>a;
          cout<<"\nHow much do you want to add to first number?: ";
          cin>>b;
          c=a+b;
          cout<<"\nThis is your answer: "<<c<<"\n"<<"Thank you for using this program!\n\n";
     }
     else if (x==3)
     {
          cout<<"\nYou chose to divide two numbers\n";
          cout<<"Enter first number: ";
          cin>>a;
          cout<<"\nHow much do you want to divide the first number by?: ";
          cin>>b;
          c=a/b;
          cout<<"\nThis is your answer: "<<c<<"\n"<<"Thank you for using this program!\n\n";
     }
     else if (x==4)
     {
          cout<<"\nYou chose to multiply two numbers\n";
          cout<<"Enter first number: ";
          cin>>a;
          cout<<"\nHow much do you want to multiply it by?: ";
          cin>>b;
          c=a*b;
          cout<<"\nThis is your answer: "<<c<<"\n"<<"Thank you for using this program!\n\n";
     }
     else if (x==5)
     {
          int d, e;
          cout<<"\nYou chose to get the modulus of two numbers\n";
          cout<<"Enter first number: ";
          cin>>d;
          cout<<"\nHow much do you want to divide the first number by?: ";
          cin>>e;
          int f=d%e;
          cout<<"\nThis is your remainder: "<<f<<"\n"<<"Thank you for using this program!\n\n";}
          cout <<"Do you want to continue using this program, enter y or n:" ;
          char contletter;
          cin >> contletter;
          if (contletter == 'y' || contletter == 'Y')
               cont = true;
          else cont = false;
          if ( ans == 'n')
          {
               cout << "Goodbye "<< endl;
          }

     } // end else
} // end while
} // end main 
Last edited on
Im afraid at the moment I cant think of anything that wouldnt involve trying to extract numbers from a string....probably not the way your teacher is wanting/expecting you to do it. Wait for the morning when one of the more experienced guys will see it, I'm sure they'll know.
Ask how many numbers they wish to use in the equation. Initialize an array of that number, or use a vector. Then ask for each number individually in a loop, then do the equation using a for loop on said array or vector.
Take a integer ,let x
input in x the numbers the user want to add.
then run a for loop till x.
closed account (z05DSL3A)
Total = 0
Input = 0
Continue = true

Do while Continue is true
{
Get input
If input is not guard
{
Total = total + Input
}
Else
{
Continue = false
}
}
Display Total


Pseudo code


The guard could be somthing like zero.
long int s,i,x,a[];
s=0;
cout<<"\n Enter no. of integers you want to add";
cin>>x;
for(i=0;i<x;i++)
{
cout<<"\n enter integers you want to add";
cin>>a[i];
s+=a[i];
}
cout<<s;

But the no. added could only be upto the range of long int;
You could combine the cases for x==1 and x==2 because the user can indicate subtraction with a minus sign
In place of a simple c>>a; use a forever loop (0 to quit) to get input and calculate running total as per Grey Wolf above.
e.g.
1
2
3
4
5
6
7
float c=0.0,sum=0.0;
for(;;)
{cout<<"Enter a number (0 to quit) ";
sin>>c;
sum+= c;
if(c=0)return sum;
}

You can do similarly with * and / and %
Topic archived. No new replies allowed.