Can I just know what's wrong with my code.

hello, I've found this question on the internet and tried to solve it, I'm a computer engineering freshman, and I thought I can try to strengthen my coding skills, so after I finished the code with no red lines, after I debug it I get around 4 errors, I just need to know what's wrong exactly and how can I fix that?
waiting for your replies ASAP, thanks =)


this is the question:
Dr Mirna always gives True/False tests to her class. Her tests have always 10 questions. The maximum class size is 5. She needs a program that will calculate the students’ grades based on the best grade.

Grade

A will range from the best score, to the best score minus 2.

B will range from the best score minus 3, to the best score minus 5.

C will range from the best score minus 6, to the best score minus 8.

F will be anything below the best score minus 8.

Each student’s ID and test answers will be entered. The output will be each student’s ID, number of correct answers, and grade, along the single highest score for the class.

Develop a C++ program for the above problem.

· Use four one-dimensional arrays – one for the correct scores and the other three for the needed output.

· Test if a student’ ID number has been entered previously before storing it in the array: each ID number cannot appear more than one time in the array.

· Check if the answer given for each question is a value equal to 0 or 1. If it is not the case, the user must re-enter the value again.

· Use functions in writing your code.







and this is my code:

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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>
using namespace std;
void Read (int ID[], int answers[], int *,int *);
void calc( int ID[], int answers[], int *,char grade[], char *, int *);
void print(int ID[],int nofcorrectanswers,int highest, char grade[]);


void Read (int ID[5], int answers[10], int *Answers, int *idnum)
{
	int IDi, int Ai,  bool stop=false;

	while(stop=false && IDi<5)
	{
		cout<<"Please enter your ID number, to stoo enter a negative value"<<endl;
		cin>>*idnum;
		if (*idnum<0)
			stop=true;
		else 
		{
			do
			{
				cout<<"Enter your answers,note that true=1,false=0"<<endl;
				cin>>*Answers;
			}
			while (*Answers);
		while (*Answers==0 && *Answers==1);
		}
		ID[IDi]=*idnum;
			answers[Ai]=*Answers;
	}
}

void calc( int ID[5], int answers[10], int *nofcorrectanswers,char grade[5], char *Grade, int *highest)
{
	int Ai, IDi,Gri;
	int correct[10]={0,1,1,0,1,0,0,1,1,0};

	for(IDi=0;IDi<5;IDi++)
	{
		for(Ai=0;Ai<10;Ai++)
		{
			if (answers[10]==correct[10])
			{
				*highest=10;
				*nofcorrectanswers=10;
			}

			else
				if(answers[10]==correct[10]-1)
				{
					*highest=9;
					*nofcorrectanswers=9;
				}
				else
					if(answers[10]==correct[10]-2)
					{
						*highest=8;
						*nofcorrectanswers=8;
					}
					else
						if(answers[10]==correct[10]-3)
						{
							*highest=7;
							*nofcorrectanswers=7;
						}
						else
							if(answers[10]==correct[10]-4)
							{
								*highest=6;
								*nofcorrectanswers=6;
							}
							else
								if(answers[10]==correct[10]-5)
								{
									*highest=5;
									*nofcorrectanswers=5;
								}
								else
									if(answers[10]==correct[10]-6)
									{
										*highest=4;
										*nofcorrectanswers=4;
									}
									else
										if(answers[10]==correct[10]-7)
										{
											*highest=3;
											*nofcorrectanswers=3;
										}
else												{
													*highest=0;
													*nofcorrectanswers=0;
												}
		}
	}


	for(Gri=0;Gri<5;Gri++)
	{
	if(*nofcorrectanswers == *highest || *nofcorrectanswers == *highest-2)
		*grade='A';
	else
		if(*nofcorrectanswers == *highest-3 || *nofcorrectanswers == *highest-5)
			*grade='B';
		else
			if(*nofcorrectanswers == *highest-6 || *nofcorrectanswers == *highest-8)
				*grade='C';
			else
				if(*nofcorrectanswers < *highest-8)
					*grade='F';
	}
	grade[Gri]=*grade;
}


void print(int ID[5],int nofcorrectanswers,int highest, char grade[5])
{
	int Ai, IDi,Gri, nofcorrectanswers;

	for(IDi=0;IDi<5;IDi++)
	{
		
		cout<<"The ID is:"<<ID[IDi]<<endl;
		for(Ai=0;Ai<10;Ai++)
		{
		cout<<"The number of correct answers is:"<<nofcorrectanswers<<endl;
		}
			cout<<"The best score is:"<<highest<<endl;
	for(Gri=0;Gri<5;Gri++)
	{
		cout<<"The grade ="<<grade[Gri]<<endl;
	}
	}

}


void main()
{
	int Answers, idnum, highest,nofcorrectanswers;
	int ID[5],answers[10];
	char grade[5], Grade;
	Read (ID, answers, &Answers, &idnum);
	calc(ID,answers, &nofcorrectanswers, grade, &Grade, &highest);
	print(ID,nofcorrectanswers,highest,grade);

	system("pause");
}





this is the result of the debugging:

1>------ Build started: Project: assignment 4 again, Configuration: Debug Win32 ------
1> assignment 4 again.cpp
1>c:\users\abbey\documents\visual studio 2010\projects\assignment 4 again\assignment 4 again\assignment 4 again.cpp(10): error C2062: type 'int' unexpected
1>c:\users\abbey\documents\visual studio 2010\projects\assignment 4 again\assignment 4 again\assignment 4 again.cpp(12): error C2065: 'stop' : undeclared identifier
1>c:\users\abbey\documents\visual studio 2010\projects\assignment 4 again\assignment 4 again\assignment 4 again.cpp(12): fatal error C1903: unable to recover from previous error(s); stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========




I'd appreciate it if I get any answers please =)
thank you!
Last edited on
first:
int IDi, int Ai, bool stop=false;

when you are creating more variables in one line make sure they are the same data type, it should be like this:
int IDi, int Ai; bool stop=false;

I've replaced "," with ";" after Ai, because it tells compiler that I've finished declaring integers and I'm going to declare boolean type variable, or it should look like this:

1
2
int IDi, int Ai;
 bool stop=false;


so you are declaring variables in separated lines by their data type

second: there is no variable with name "stop" because you had an error while declaring it, fixing first problem will fix also this one
Last edited on
You go about declaring your variables the wrong way. You can only chain declarations of the same type together:
1
2
int x,y;
bool z; //Note how bool needs its own line 

Also, your while code should be:
1
2
while(stop == false && ...)
//You must use the equality operator ("==") and not the assignment operator('=') 


EDIT: Grammar
Last edited on
:error C2062: type 'int' unexpected
This tells you there is something wrong with a declaration. Look at line 10:int IDi, int Ai, bool stop=false;

:error C2065: 'stop' : undeclared identifier
This tells you there is something wrong with the declaration of the variable stop, it was never defined. Look at line 12.

Edit: I tried to be fast, made mistakes and still got beat by two others. You guys are too good.

And mekkatorqu, it should beint IDi, Ai;orint IDi; int Ai;

Edit2: I also got these errors after correcting your first few errors.
||In function 'void print(int*, int, int, char*)':|
|118|error: declaration of 'int nofcorrectanswers' shadows a parameter|
|138|error: '::main' must return 'int'|
||In function 'int main()':|
|147|error: 'system' was not declared in this scope|
||=== Build finished: 3 errors, 0 warnings ===|
Last edited on
Fix line 12:

while(stop=false && IDi<5)
needs to be
while (stop == false && IDi < 5)

Also, if I'm not mistaken, you never initialize IDi to anything before you check its value. That's going to give you an issue eventually as well.
Thank you A lot !! , I have no idea how did that slip from my mind!
after I fixed it, I'm getting "1 succeeded and 0 failed"
but still two errors written, and when I debug it, it just sticks on the
"Enter your answers,note that true=1,false=0"
and it's accepting all values not only zero and one, how can I fix that?


This is the code after I changed it:
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
using namespace std;
void Read (int ID[], int answers[], int *,int *);
void calc( int ID[], int answers[], int *,char grade[], char *, int *);
void print(int ID[],int nofcorrectanswers,int highest, char grade[]);


void Read (int ID[5], int answers[10], int *Answers, int *idnum)
{
	int IDi;
	int Ai;
	bool stop=false;
	IDi=0;
	while(stop==false && IDi<5)
	{
		cout<<"Please enter your ID number, to stop enter a negative value"<<endl;
		cin>>*idnum;
		if (*idnum<0)
			stop=true;
		else 
		{
			do
			{
				cout<<"Enter your answers,note that true=1,false=0"<<endl;
				cin>>*Answers;
			}
			while (*Answers);
		while (*Answers==0 && *Answers==1);
		}
		ID[IDi]=*idnum;
			answers[Ai]=*Answers;
	}
}

void calc( int ID[5], int answers[10], int *nofcorrectanswers,char grade[5], char *Grade, int *highest)
{
	int Ai, IDi,Gri;
	int correct[10]={0,1,1,0,1,0,0,1,1,0};

	for(IDi=0;IDi<5;IDi++)
	{
		for(Ai=0;Ai<10;Ai++)
		{
			if (answers[10]==correct[10])
			{
				*highest=10;
				*nofcorrectanswers=10;
			}

			else
				if(answers[10]==correct[10]-1)
				{
					*highest=9;
					*nofcorrectanswers=9;
				}
				else
					if(answers[10]==correct[10]-2)
					{
						*highest=8;
						*nofcorrectanswers=8;
					}
					else
						if(answers[10]==correct[10]-3)
						{
							*highest=7;
							*nofcorrectanswers=7;
						}
						else
							if(answers[10]==correct[10]-4)
							{
								*highest=6;
								*nofcorrectanswers=6;
							}
							else
								if(answers[10]==correct[10]-5)
								{
									*highest=5;
									*nofcorrectanswers=5;
								}
								else
									if(answers[10]==correct[10]-6)
									{
										*highest=4;
										*nofcorrectanswers=4;
									}
									else
										if(answers[10]==correct[10]-7)
										{
											*highest=3;
											*nofcorrectanswers=3;
										}
										else
												{
													*highest=0;
													*nofcorrectanswers=0;
												}
		}
	}


	for(Gri=0;Gri<5;Gri++)
	{
	if(*nofcorrectanswers == *highest || *nofcorrectanswers == *highest-2)
		*grade='A';
	else
		if(*nofcorrectanswers == *highest-3 || *nofcorrectanswers == *highest-5)
			*grade='B';
		else
			if(*nofcorrectanswers == *highest-6 || *nofcorrectanswers == *highest-8)
				*grade='C';
			else
				if(*nofcorrectanswers < *highest-8)
					*grade='F';
	}
	grade[Gri]=*grade;
}


void print(int ID[5],int nofcorrectanswers,int highest, char grade[5])
{
	int Ai;
	int IDi;
	int Gri;
	
	for(IDi=0;IDi<5;IDi++)
	{
		
		cout<<"The ID is:"<<ID[IDi]<<endl;
		for(Ai=0;Ai<10;Ai++)
		{
		cout<<"The number of correct answers is:"<<nofcorrectanswers<<endl;
		}
			cout<<"The best score is:"<<highest<<endl;
	for(Gri=0;Gri<5;Gri++)
	{
		cout<<"The grade ="<<grade[Gri]<<endl;
	}
	}

}


void main()
{
	int Answers, idnum, highest,nofcorrectanswers;
	int ID[5],answers[10];
	char grade[5], Grade;
	Read (ID, answers, &Answers, &idnum);
	calc(ID,answers, &nofcorrectanswers, grade, &Grade, &highest);
	print(ID,nofcorrectanswers,highest,grade);

	system("pause");
}





and this is the result of the compile :


1>------ Build started: Project: assignment 4 again, Configuration: Debug Win32 ------
1>  assignment 4 again.cpp
1>c:\users\abbey\documents\visual studio 2010\projects\assignment 4 again\assignment 4 again\assignment 4 again.cpp(31): warning C4700: uninitialized local variable 'Ai' used
1>c:\users\abbey\documents\visual studio 2010\projects\assignment 4 again\assignment 4 again\assignment 4 again.cpp(44): warning C4700: uninitialized local variable 'correct' used
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
Change this:
1
2
		if (*idnum<0)
			stop=true;


To:
1
2
		if (*idnum<1) // Allows 1 or higher
			stop=true;


Also this:
1
2
3
4
	int IDi;
	int Ai;
	bool stop=false;
	IDi=0;


Should be something like:
1
2
3
	int IDi = 0;
	int Ai = 0;
	bool stop=false;


And at lines 44-87, I believe instead of:
1
2
3
4
5
			if (answers[10]==correct[10])
			{
				*highest=10;
				*nofcorrectanswers=10;
			}


You want something like:
1
2
3
4
5
			if (answers[Ai]==correct[Ai])
			{
				*highest=10;
				*nofcorrectanswers=10;
			}


This last segment is a little confusing as to what you're specifically doing, but using an array that has 10 values in it, you shouldn't declare that array [10], it should have given you an error saying out of range, or something similar. Instead, I believe that you wanted to loop through the array.

Also, here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
		cout<<"Please enter your ID number, to stop enter a negative value"<<endl;
		cin>>*idnum;
		if (*idnum<0)
			stop=true;
		else 
		{
			do
			{
				cout<<"Enter your answers,note that true=1,false=0"<<endl;
				cin>>*Answers;
			}
			while (*Answers);
		while (*Answers==0 && *Answers==1);
		}
		ID[IDi]=*idnum;
			answers[Ai]=*Answers;
	}


You ask the user for their ID, let them enter one answer, then ask for the ID again...Is that what you want? I would use another loop within the else to allow someone to enter all ten answers in a row. Also the error checking for the ID needs to be rewritten, I was allowed to put in user ID of 5 or higher. There is quite a bit of code that needs to be reworked to get the results that you're actually looking for.
Last edited on
Also, looking over the code, you declare an array to hold 5 different IDs, did you want to do this? or did you want to allow for an ID value of 1-5? I think you're off to a good start, but the program overall needs to be rethought about what you're trying to do, comment each of your lines so you know what you're doing, and so we know as well. The best way to help you is by understanding what your code is actually supposed to do. I hope this helps, and if you want a little more 1 on 1 you can pm me and I can see how much more I can help you.
What I want is, ask the user to enter an ID, then ten answers, and the same thing again and again until it's 5 IDs entered and for each ID 10 answers, and those answers are only 0s and 1s,,, also to make sure than the user cannot enter the same ID twice.
Two of the biggest issues I see you running into is that you allowed 5 different ID's to be entered, which is right, but you only allow 10 answers between all 5 IDs, which allows 2 answers each. You will need a multidemensional array, Answers[5][10], this allows the ID's to be cross referenced using the ID[5] array. As it stands now, your program can't handle the multidemensional array.
Here is what I've done with your Read function, it's not the prettiest, but it works like I believe you wanted. I had to define a new array in your main as so, int Answers2[5][10]
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

void Read(int ID[5], int Answers[5][10]) {
    int userID, Answer, IDs = 0;
    bool stop = false;

    // Allow 5 ID Entries
    do {
        // Get Each ID Entry
        do {
            cout << "Please enter ID number " << (IDs + 1) << " (to stop, enter a negative value)" << endl;
            cin >> userID;

        // Loops if the user entered 0
        } while(userID == 0);
        // Checks to make sure the user entered a positive number
        if(userID > 0) {
            // Allows 10 answers
            for(int i = 0; i < 10; i ++) {
                // Make sure the answer is 1 or 0
                do {
                    cout << "Enter Answer " << (i + 1) << " (True = 1, False =0)" << endl;
                    cin >> Answer;
                } while((Answer != 1) && (Answer != 0));
                // Assign Answer to Answers
                Answers[IDs][i] = Answer;
                ID[IDs] = userID;
            }
        }
        // Check if the user entered a negative number
        else if(userID < 0)
            stop = true;
    // Loop until stop is true or 5 IDs have been entered
    } while(!stop && IDs < 5);
}


I commented the parts that might have been a little harder to understand and I hope this helps you get back onto the right track. Any more questions, feel free to ask.

Edit: Please pardon my ignorance, I just went back to the first line and saw you needed four different one dimensional arrays. That makes my code not allowed.
Last edited on
It's alright, that way of yours makes it a whole a lot easier ! , but the question is asking for 1D array.
Yeah I noticed that and felt like a dummy after posting it, my apologies, but I went ahead and made a template that you can use to aide in redoing the code. I believe you'll find it much easier to use. Worth a try anyways. Here is my sample. You've already done a lot of the things, so you can almost copy your code over, just need to change a few variables and rethink some of your variable usage.
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
// Preprocessor Directives
// Don't forget your includes here

// Declare Global Arrays
int IDs[5]; // ID's for each student
int Scores[5]; // Number of correct Answers
int Grade[5]; // Final Grade
int CorrectAnswers[10] = {0,1,1,0,1,0,0,1,1,0}; // Teacher's answers
int HighestGrade = 0;

// Declare Function Prototypes
void EnterIDs();
void EnterAnswers();
void GetGrades();
void Display(int);

int main() {
    // Enter the ID's in
    // Set Student's Grades
    // Display all of the grades

    return 0;
}

void EnterIDs() {
    // Loop to input ID's
    // Allow 5 ID's to be entered, must be positive
        //· Test if a student’ ID number has been entered previously
            //before storing it in the array: each ID number cannot
            //appear more than one time in the array.

        // Within Loop, Enter Answers
}

void EnterAnswers() {
    // Loop to input all 10 answers
        // 
        //· Check if the answer given for each question is a value
            //equal to 0 or 1. If it is not the case,
            //the user must re-enter the value again.
        // Check Answer with Correct Answer
        // Tally if answer is right

    // Set Score with number right
}

void GetGrades() {
    // Set up grading scale, find highest score through for loop
        // From highest score, Assign Grades
            /*  A will range from the best score, to the best score minus 2.
                B will range from the best score minus 3, to the best score minus 5.
                C will range from the best score minus 6, to the best score minus 8.
                F will be anything below the best score minus 8. */
}

void Display() {
    // Loop to output
        // Print out Student ID, Number of correct answers, Grade,

    // Print out the Highest Grade (Possibly with Student ID?)
}
Last edited on
Topic archived. No new replies allowed.