infinite loop,

Jul 29, 2011 at 12:55pm
what's the problem with this one, it seems that everything is fine until i enter the second input after i entered it it will have an infinite loop,

#include<iostream>
#include<string>
using namespace std;

struct fraction{
string in1;
string in2;
int add;
int minus;
int times;
int divide;
};

void addition(fraction, fraction);

void main(){
string frac1;
string frac2;
int choice;

do{
cout<<"1 Addition"<<endl;
cout<<"2 Subtraction"<<endl;
cout<<"3 Multiplication"<<endl;
cout<<"4 Division"<<endl;
cout<<"5 EXIT"<<endl;
cout<<"enter number of choice"<<endl;

cin>>choice;

switch(choice){

case 1:
cout<<"Addition"<<endl;
getline(cin,frac1);
getline(cin,frac2);
cin.ignore();
break;
case 2:
cout<<"Subtraction"<<endl;
break;
case 3:
cout<<"Multiplication"<<endl;
break;
case 4:
cout<<"division"<<endl;
break;
case 5:
cout<<"exit"<<endl;
break;
default:
cout<<"INVALID NUMBER OF CHOICE, CHOOSE AGAIN"<<endl;
cin>>choice;
break;
}
}while(choice!=5);
}

void addition(fraction num1, fraction num2){

}
Jul 29, 2011 at 1:38pm
It could be that getline() or cin is corrupt. Put in defensive code to see if that is the case (check the fail bit of cin.)
Jul 29, 2011 at 2:21pm
Hi, I've just looked at your code. Firstly, could you please use code tags so that you code will be more readable! [ code ] //your code [ /code ] becomes //your code .

You loop behavior seems to be fine, I've just looped through your application several times before keying in 5 to exit. Having said that your code does contain a few oddities.

One problem stems from the use of getline(...). When you use at your first cin statement, whatever value you read in, for example "1" when you press the Enter key, the value one is taken and stored in the appropriate variable (in this case choice).

As the Enter (or Return) key is also a type of character it remains on the stream, then this is consumed by the first getline(...) call. That's why frac1 is always empty.

You can solve this by using cin instead of getline(...).

zhar999 wrote:
void main()
Use of the void main() prototype is not good practice in C++. You should use:

1
2
3
4
5
6
int main()
{
    //your code

    return 0;
}


Inside your case statement you have an additional cin which is completely pointless, as when you loop again, you are asked to re-enter your "choice".

Lastly void addition(fraction, fraction); I've seen these kinds of function prototypes before and it always surprises me that it compiles. I can't really comment much on it other than the fact that I would avoid these kinds of prototypes as you cannot tell what the data type is, but whether you take this onboard is your choice (see what I did there).

Oh also (I've only just noticed), the function prototype void addition(fraction, fraction); and the function definition void addition(fraction num1, fraction num2) do not match. I'm pretty sure the compiler will treat them as different functions. Although I'm not quite sure if that was intended...

I hope this helps.
Jul 29, 2011 at 2:23pm
what do you mean by defensive code?
Jul 29, 2011 at 2:27pm
You should follow lnk2019's advice. By defensive code I mean check for errors: As I said, check the fail bit of cin.

1
2
3
4
5
6
7

if( cin.fail() )
    {
     /*    Warn the user or try to recover ...    */

    }    /*    if( cin.fail() )    */
Jul 29, 2011 at 2:30pm
thank you Ink, i got it, i just put cin.ignore() after the the 1st cin, thanks! =)
Jul 29, 2011 at 3:11pm
okay gotta question....how do you guys put source code into your post?? just started a forum account...just getting used to the basics..thanks
Jul 29, 2011 at 3:19pm
i also don't...i think i can practice here, :D

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
#include<iostream>
#include<string>
using namespace std;

struct fraction{
    string in1;
    string in2;
    int add;
    int minus;
    int times;
    int divide;
};

//fraction addition(fraction, fraction);

void main(){
	string frac1;
	string frac2;
	int store1[10];
	int store2[10];
	int *ptr1=&store1[10];
	int *ptr2=&store2[10];
    int choice;
	int i=0;
	int k=0;

    do{
        cout<<"1 Addition"<<endl;
        cout<<"2 Subtraction"<<endl;
        cout<<"3 Multiplication"<<endl;
        cout<<"4 Division"<<endl;
        cout<<"5 EXIT"<<endl;
        cout<<"enter number of choice"<<endl;
    
        cin>>choice;
		cin.ignore();

        switch(choice){
            
        case 1:
            cout<<"Addition"<<endl;
            getline(cin,frac1);
            getline(cin,frac2);
			for(;i!=frac1.length();++i){
				cout<<"ascii: "<<int(frac1[i])<<endl;
				if(i=='\0')
					i=0;
			}
			for(;k!=frac2.length();++k){
				cout<<"ascii: "<<int(frac2[k])<<endl;
				if(k=='\0')
					k=0;
			}
			while(frac1[i]!=47){
				*ptr1=frac1[i];
				i++;
				ptr1++;
			}
            break;
        case 2:
            cout<<"Subtraction"<<endl;
            break;
        case 3:
            cout<<"Multiplication"<<endl;
            break;
        case 4:
            cout<<"division"<<endl;
            break;
        case 5:
            cout<<"exit"<<endl;
            break;
        default:
            cout<<"INVALID NUMBER OF CHOICE, CHOOSE AGAIN"<<endl;
            cin>>choice;
            break;
        }
    }while(choice!=5);
}

/*void addition(fraction num1, fraction num2){

}*/
Jul 29, 2011 at 3:20pm
wow!
Jul 29, 2011 at 3:22pm
there's something wrong with that code, i want to get the numerator and denaminator from the string i entered,
Jul 29, 2011 at 3:33pm
@AppDevTrainee: Use the code blocks, found under the Format: box to the right.
Jul 29, 2011 at 3:50pm
thanks man
Topic archived. No new replies allowed.