I need some explanation on what i did wrong

So, i have created a program, which starts with a loop that asks the user to input his/her name and if the user inputs '-1' the loop will stop. My problem is when the loop runs for the 2nd time, the loop doesn't asks for the user's name and goes straight to the function enterValue().

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
#include<iostream>
#include<string>
using namespace std;
void enterValue(int,char,int&);
void totalScore(int,int&);
int main()
{
	int num[3]={1,2,3},score[3]={0,0,0},a = 0,total_student = 0,totalavg = 0,totalpoint = 0,equal = 0;
	bool stop = true, run = false,program;
	char name[30],desc[3]={'P','F','C'};

	while(name[0] !='-' && name[1] != '1')
	{
		a = 0;
		cout<<"Enter your name(Enter -1 to exit.): ";
		cin.getline(name,30);
		
		if (name[0] =='-' && name[1] == '1')
		a = 3;
		
		while(a<3)
		{				
			enterValue(num[a],desc[a],score[a]);
			totalScore(score[a],totalpoint);
			total_student++;
			a++;
		}	
	}
	
	totalavg = ((totalpoint/3)/total_student);
	cout<<"\n\tEvaluation of the System\n\t ";
	while(equal < 32)
	{
		cout<<"=";
		equal++;
	}
	cout<<"\n\tNumber of students who answered: "<<total_student;
	cout<<"\n\tTotal Score Collected: "<< totalpoint;
	cout<<"\n\tTotal Average: "<< totalavg;
	cout<<"\n\tThe level of Satisfaction: ";
	if ( totalavg >= 7)
	cout<<"Excellent";
	else if (totalavg > 4 &&  totalavg < 7)
	cout<<"Moderate";
	else
	cout<<"Poor";
	
	return 0;
}
void totalScore(int a,int& b)
{
	b += a;
}
void enterValue(int a,char b,int& c)
{
	switch(b)
	{
		case 'P':
			if (c < 0 || c > 10)
			c = 0;
			break;
			cout<<"Enter score for Software Performance: ";
			cin>>c;
			break;

		case 'F':
			if (c < 0 || c > 10)
			c = 0;
			break;
			cout<<"Enter score for User Friendliness: ";
			cin>>c;
			break;
		case 'C':
			if (c < 0 || c > 10)
			c = 0;
			break;
			cout<<"Enter score for System Capabilities: ";
			cin>>c;
			break;
	}
}
Last edited on
A few problems.

To begin, your char array, name, is unintialized. You can initialize it as char name[30] = {} to initialize all elements to 0 (AKA null character '\0').

Second, you should use proper indentation for clarity
ex,
1
2
	if (c < 0 || c > 10)
	c = 0;

should be
1
2
	if (c < 0 || c > 10)
		c = 0;


Third,
1
2
3
4
5
6
7
		case 'P':
			if (c < 0 || c > 10)
			    c = 0;
			break;
			cout<<"Enter score for Software Performance: ";
			cin>>c;
			break;

Everything after your first break will never be reached. Your user will never be able to enter a score for Software Performance.
(Although the logic here doesn't make much sense, why would you manually assign c right before using cin to assign it?)


Fourth, what happens when user enter "-1" as their first input? total_student will be 0, and you will therefore be dividing by 0 at totalavg = ((totalpoint/3)/total_student);

Which loop running for a second time do you mean? The outer loop or the inner loop? Looks like the inner loop runs 3 times in a row if the user does not enter -1 (but of course doesn't do anything because of the problems in enterValue).

So try to fix those problems and then see what problems remain.

Edit: Almost forgot,
If you're still having input problems, it's because of the mixing of cin >> and getline.
See: https://stackoverflow.com/questions/12186568/getline-skipping-first-even-after-clear
Last edited on
Thank you so much for the quick response !

I have fixed the program according to your comments.I have fixed the logic error which occured in function enterValue and i have also fixed the indentations. I have also learned some new things for example the use of cin.ignore() and initializing all elements of an array to 0 by using \0'.

The program is running perfectly according to how i wanted it to run. Thanks !
Topic archived. No new replies allowed.