infinite loop problem

Sep 29, 2014 at 8:03pm
working on "ex 3" at bottom of code
I am trying to write a coin toss program that uses rand() to come up with the number 1 or 0.
Each one represents heads or tails.
The program asks the user "coin toss?"
The user inputs either yes or no
no stops the program
yes generates a coin toss with either heads or tails, then asks the user "coin toss".
Right now my while loop will not stop, and it goes into an infinite loop even when "no" is entered?
Why is this?
If no is entered it should not even execute the loop right?


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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>

using namespace std;

int main ()
{
    srand(time(0));
    char ch;
    int ex;
    cout<<"Which excerise? ";
    cin>>ex;
    cout<<endl;
    if (ex == 1)
    {
            double beg, end;
            int num;
        
            cout<<"Enter beginning and ending numbers, seperated by space: "<<endl;
            cin>>beg;
            cin>>end;
            cout<<endl;
    
            for (num=beg; num<=end; ++num)
            {
                    cout<<num<<","<<" ";
            }
            cout<<endl;
            cout<<endl;
    
            cout<<"Enter beginning and ending numbers, seperated by space: "<<endl;
            cin>>beg;
            cin>>end;
            cout<<endl;
    
            for (num=beg; num<=end; num++)
            {
                    cout<<num<<","<<" ";
                    num+=1;
            }
            cout<<endl;
            cout<<endl;
    
            cout<<"Enter the beginning and largest positive numbers, seperated by space: "<<endl;
            cin>>beg;
            cin>>end;
            cout<<endl;
    
            for (num=beg; num<=end; num)
            {
                    cout<<num<<","<<" ";
                    num*=-2;
            }
            cout<<endl;
            cout<<endl;
    }
    if (ex == 2)
    {
            int grade;
            double A, B, C, D, F;
            A=0;
            B=0;
            C=0;
            D=0;
            F=0;
    
            cout<<"Enter one or more grades, or -1 to stop:"<<endl;
        
            do 
            {
                    cin>>grade;    
        
                    if (grade>=90) A++;
			        if ((grade>=80) && (grade<=89)) B++;
			        if ((grade>=70) && (grade<=79)) C++;
			        if ((grade>=60) && (grade<=69)) D++;
			        if ((grade>=0) && (grade<=59)) F++;
            }
            while (grade!=-1);
        
            
            {
                    cout<<"The grades breakdown is:"<<endl;
                    cout<<"A's: "<<A<<endl;
                    cout<<"B's: "<<B<<endl;
                    cout<<"C's: "<<C<<endl;
                    cout<<"D's: "<<D<<endl;
                    cout<<"F's: "<<F<<endl;
            }
    }
    if (ex == 3)
    {
            double again, yes;
            int heads, tails;
        
            cout<<"Toss the coin? "<<endl;
            cin>>again;
            
            while (again==yes)
            {   
                    int random_number = rand()%2;
                
                    if (random_number==0)
                    {
                            cout<<"Heads"<<endl;
                    }
                    if (random_number==1)
                    {
                            cout<<"Tails"<<endl;
                    }
            }
            
    }

    
return 0;
}
Sep 29, 2014 at 8:05pm
do I need a break; after the if statements?
Sep 29, 2014 at 8:12pm
What are yes and again's type?
Sep 29, 2014 at 8:16pm
I managed to end the infinite loop but now if the user inputs yes for coin toss it only toss once and does not ask the user for another coin toss, it does the same thing 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
if (ex == 3)
    {
            double again, yes, no;
            int heads, tails;
        
            cout<<"Toss the coin? "<<endl;
            cin>>again;
            
            do
            {
                    
                    int random_number = rand()%2;
                
                    if (random_number==0)
                    {
                            cout<<"Heads"<<endl;
                            break;
                    }
                    if (random_number==1)
                    {
                            cout<<"Tails"<<endl;
                            break;
                    }
            }
            while (again==yes);
            
    }

    
return 0;
}
Sep 29, 2014 at 8:19pm
You only got your problem worse.
Sep 29, 2014 at 8:30pm
your right, I went back to original code.
yes and again are doubles, no?
Sep 29, 2014 at 8:31pm
back to infinite loop :(
Sep 29, 2014 at 8:46pm
Yes, they are doubles. Nothing wrong with this, "right?"
Sep 29, 2014 at 9:41pm
but now I just get the infinite loop no matter if "no" or "yes" is selected.
Sep 29, 2014 at 11:14pm
now is good time to improve your debugging skills! :)
what the 'again' variable gets from the user?
the user input needs to be part of the loop, to be able to exit the loop..
Last edited on Sep 29, 2014 at 11:20pm
Sep 29, 2014 at 11:32pm
Thanks igor I did put the cout and cin statements in the loop and the loop still does not stop it only displays the cout and continues in its infinite madness
Sep 29, 2014 at 11:38pm
so debug it step by step, put a watch on the 'again' variable to check what it conteins for the loop duration..
Sep 30, 2014 at 8:20am
Take it one step at a time.
What is a double? How can a double store a yes/no?
Topic archived. No new replies allowed.