bug on a simple c++ program

Write your question here.

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
// ASKISEIS VOITHIMATOS.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <string>
#include <iostream>

using namespace std;
int main()

{




	int tot(0);int sum(0);
int bp; int bg;
int vath=0;
int sinolo(0),over18(0);


string onoma;
getline(cin,onoma);



while ( onoma!= " ")

{
sinolo++;


	cin>>bp;
		while (bp>20 || bp<0)
	{cout<<"lathos proforikos vathmos"<<endl;cin>>bp;}
	

		cin>>bg;
		while (bg>20 || bg<0)
	{cout<<"lathos graptos vathmos"<<endl;cin>>bg;}
	
	
		vath=bg*0.7 + bp *0.3;
			cout<<onoma<<"             "<<vath<<endl;
			if (vath>18  )
			over18++;

			cout<<"grapse onoma"<<endl;

	getline(cin,onoma);   //problem here


}

if (sinolo!=0)
	cout<<over18/sinolo<<"   panw apo 18"<<endl;




system("PAUSE");


return 0;
}



the application doesnt request the 'onoma' variable inside the loop (the getline(cin,onoma) command doesnt work)
so at the "while(onoma !=" ")" the variable 'onoma' always appears to be "" and thus the program continues forever...
how can I solve this ?
thank you very much !

Replace getline(cin,onoma); with cin >> onoma;
Last edited on
according to the site's tutorial at http://www.cplusplus.com/files/tutorial.pdf



"cin extraction stops reading as soon as if finds any blank space character, so in this
case we will be able to get just one word for each extraction. This behavior may or may not be what we want; for
example if we want to get a sentence from the user, this extraction operation would not be useful. "


so the cin will be a bad choise when trying to read a name
however, I have tried it and every time I type a space character and press enter it doesnt move to the next command
actually it does nothing.it just waits for me to type something else,and the in proceeds to check the 'while' condition
Topic archived. No new replies allowed.