Help with beginner program

I am not going to try to hide the fact that this is school work. There are still problems with the code that I am ironing out but the most glaring is the fact that when I enter Y to loop the code back to the beginning,there is not a prompt to input the diver's name again.It prompts the first time but not on sucessive runs. I have the impression that it is the getline command used. This is the first time I have used the syntax so I am not sure if I used it correctly.

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
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{  
	string diver_name;    //declarations
	string diver_city;
	double score;
	double high_score=0;
	double low_score=0;
	double total_score=0;
	double final_score;
	int judge_number=1;
	double degree=0;
	int diver_counter=1;
	bool another_diver=true;
	char process_again;

	
	while (another_diver == true)
	{

	cout<<"Report to the media"<<endl;       //Title header
	cout<<"Event: Diving Competition"<<endl<<endl;

	begin:

	cout<<fixed<<setprecision(2);   //output formatting         

	cout<<"Divers name: ";     //get input
	getline (cin, diver_name);
	cout<<endl;
	cout<<"Divers hometown: ";
	getline (cin, diver_city);

	for (judge_number=1; judge_number<6; judge_number++)
	{
		cout<<"Enter the score given by Judge #"<<judge_number<<": ";
		cin>>score;

			while (score<0 || score>10)
			{
				cout<<"Score must be between 0 and 10"<<endl;
				cout<<"Enter the score given by Judge #"<<judge_number<<": ";
				cin>>score;
			}

		if (score < low_score)
				{
					low_score = score;
				}

		if (score > high_score)
				{
					high_score = score;
				}

					total_score += score;
			

		

	}	
	

	degree_of_difficulty:
	cout<<"Please enter the dive's degree of difficulty: ";
	cin>>degree;

		if (degree < 1.00 || degree > 1.67)
		{
				cout<<"Degree of difficulty must be between 1.00 and 1.67";
				goto degree_of_difficulty;
		}

	final_score = (total_score-high_score-low_score)/3;
	final_score = final_score*degree;


	cout<<"Diver: "<<diver_name<<"\tCity: "<<diver_city<<endl;
	cout<<"Diver's final score: "<<final_score<<endl;

	cout<<"Press Y to process another diver or N to close the program"<<endl;
	cin>>process_again;

	if ((process_again='y') || (process_again='Y'))
	{
		another_diver=true;
		goto begin;
	}

	else
		goto end;
	}

	end:

	cin.get();
	cin.get();
	return 0;	
}
Last edited on
Sigh, another one of these. But please don't think I am talking about you personally, it is just that this appears so often here. I just don't know why people can't find it already in Google. I'll try a search myself. I'll try "c++ console no prompt"...

Ok, 5th result http://stackoverflow.com/questions/2209135/safely-prompt-for-yes-no-with-cin is relevant but too obscure to decipher in this context.

First page gone by, no direct hit. I'll try with "c++ console skips input"...

OK, 4th hit was good enough: http://www.dreamincode.net/forums/topic/187571-cingetline-making-the-console-skip-a-line/. It took me some 12 minutes. Not too bad. Multiply that by 3 just to be on the safe side and you still have a decent time span, wouldn't you say?

Anyway, I never remember exactly how the whole thing goes because I don't do console apps, so pardon if my solution here is not 100% spot on:

This happens because the newline char is lingering on inside the keyboard input buffer, which is the source of data for 'cin'. What does this mean? It means that the getline() function will find it right away without you having to type a thing, and therefore "skipping" the prompt.

Fixes:

1. cin.sync() I believe does the trick pretty well. You should call it every time after a call to the extraction operator (operator>>).
2. I think cin.ignore() will ignore one char, so if the next char in line is the newline char, this works too.
3. If you want to ensure more chars are ignored, you can use the overloaded method that takes the count of chars to ignore, plus an escape char. I think it is cin.ignore(std::numeric_limits<streamsize>::max(), '\n');. To be able to use std::numeric_limits, you need to #include <limits>.

If one of the above doesn't work, it could be because cin's fail bit may be on. Call cin.clear() before the solution you picked above in order to restore cin to a good state.
Thank you for the reply. I know that I should have found it on my own. I was just stressed out over not being able to figure it out and obviously was not thinking straight. I have since revised the code and am nearing completion of the program. I am now having a problem with the calculation phase of the program. When the program calculates it needs to drop the highest and lowest score then average the remaining three. This number is then multiplied by the degree of difficulty number. I believe the problem to be with lines 50-58.

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
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{  
	string diver_name;    //declarations
	string diver_city;
	double score;
	double high_score;
	double low_score;
	double total_score=0;
	double final_score;
	int judge_number=1;
	double degree=0;
	char process_again;


	cout<<"Report to the media"<<endl;       //Title header
	cout<<"Event: Diving Competition"<<endl<<endl;


	cout<<fixed<<setprecision(2);   //output formatting         

	start:
	cin.sync();
	cout<<"Divers name: ";     //get input
	getline (cin, diver_name);
	cout<<"Divers hometown: ";
	getline (cin, diver_city);

	for (judge_number=1; judge_number<6; judge_number++)
	{
		cout<<"Enter the score given by Judge #"<<judge_number<<": ";
		cin>>score;

			while (score<0 || score>10)
			{
				cout<<"Score must be between 0 and 10"<<endl;
				cout<<"Enter the score given by Judge #"<<judge_number<<": ";
				cin>>score;
			
			}

			low_score=0;
			high_score=0;

			if (score <= low_score)
				{
					low_score = score;
				}

			if (score >= high_score)
				{
					high_score = score;
				}
				
	
			
		
				total_score += score;

	}	
	

	degree_of_difficulty:
	cout<<"Please enter the dive's degree of difficulty: ";
	cin>>degree;

		if (degree < 1.00 || degree > 1.67)
		{
				cin.sync();
				cout<<endl;
				cout<<"Degree of difficulty must be between 1.00 and 1.67";
				cout<<endl;
				goto degree_of_difficulty;
		}

	final_score = (total_score-high_score-low_score)/3;
	final_score = final_score*degree;


	cout<<"Diver: "<<diver_name<<endl;
	cout<<"City: "<<diver_city<<endl;
	cout<<"Diver's final score: "<<final_score<<endl;

	cout<<"Press Y to process another diver or N to close the program"<<endl;
	cin>>process_again;

	if ((process_again=='y') || (process_again=='Y'))
	{
		goto start;

	}
	
	else 
	{
		goto end;
	}	
	

	end:

	return 0;	
}

As an aside any input on how to clean the code up a bit would be of tremendous help. This is my first C++ class and I am a bit overwhelmed.
Last edited on
Topic archived. No new replies allowed.