please help

Pages: 12
You've specified answers to be an array of 19 elements, and then you've tried to put 20 elements in it.

Also, [ does not go with }
test_answers[count}
Last edited on
I changed all initializations to 20, but that didn't get rid of the errors. I fixed a red line by initalizing a variable i had that wasn't already initalized. number_of_correct_answers but I still have red lines under a cin all count variables and the else
I reduced the errors to not as much and simplar ones but can't figure out why they're occurring here's my code as it stands.
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
#include <iostream>
using namespace std;

int main()
{
	const int test_questions = 20;
	const int test_answers = 20;
	char answers = 20;
	int number_of_correct_answers;

	char answers[test_answers] = { 'B', 'D', 'A', 'A',
		'C', 'A', 'B', 
		'A', 'C', 'D', 'B', 'C', 'D', 
		'A', 'D', 'C', 'C', 'B', 'D', 'A'};
char test_answers[test_questions];
int test_answers[count];
cout << "what were the student's answers\n";
cout << "type the answers with a space in between\n";
test_answers[count] = toupper(test_answers[count]);
	cin >> test_answers[test_questions];

for (count = 0; test_answers[count] == answers[count]; count++)
number_of_correct_answers = number_of_correct_answers + 1;
if (number_of_correct_answers = 11)
cout << "the student passed\n";
else
cout << "the student failed\n";
return 0;
} 

and here are the errors.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
1>  array program.cpp
1>l:\comp sci stuff\array program\array program.cpp(11): error C2040: 'answers' : 'char [20]' differs in levels of indirection from 'char'
1>l:\comp sci stuff\array program\array program.cpp(15): error C2373: 'test_answers' : redefinition; different type modifiers
1>          l:\comp sci stuff\array program\array program.cpp(7) : see declaration of 'test_answers'
1>l:\comp sci stuff\array program\array program.cpp(17): error C2065: 'count' : undeclared identifier
1>l:\comp sci stuff\array program\array program.cpp(17): error C2373: 'test_answers' : redefinition; different type modifiers
1>          l:\comp sci stuff\array program\array program.cpp(7) : see declaration of 'test_answers'
1>l:\comp sci stuff\array program\array program.cpp(20): error C2065: 'count' : undeclared identifier
1>l:\comp sci stuff\array program\array program.cpp(20): error C2065: 'count' : undeclared identifier
1>l:\comp sci stuff\array program\array program.cpp(21): error C2109: subscript requires array or pointer type
1>l:\comp sci stuff\array program\array program.cpp(23): error C2065: 'count' : undeclared identifier
1>l:\comp sci stuff\array program\array program.cpp(23): error C2065: 'count' : undeclared identifier
1>l:\comp sci stuff\array program\array program.cpp(23): error C2065: 'count' : undeclared identifier
1>l:\comp sci stuff\array program\array program.cpp(23): error C2065: 'count' : undeclared identifier
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
You can only have one variable with any one name.

Something like this:
1
2
int x;
double x;

would be forbidden, as objects cannot have the same name, and both these objects are called x.

You have this:

1
2
3
4
const int test_answers = 20;  // an integer named test_answers
...
char test_answers[test_questions]; // an array of char, named test_answers
int test_answers[count]; // an array of int, named test_answers 

You've tried to make three different things with the same name, test_answers. You can only have one thing named test_answers.
Last edited on
test_answers[count] = toupper(test_answers[count]);

and also this... it just wrong... it wasn't on a loop...

what do you need is the understanding of basics of C++, which you can get in this site's tutorial...
char answers = 20; is defined incorrectly.

It is also a lot easier when you are programming to define all the variables you are gonna use in main() at the top of the function. In addition to that, a lot of your variables are equal, like int test_answers and int test_questions. It makes it easier if you just use one variable repeatedly. I put some comments in your code to help you define your variables.

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

int main()
{
	const int test_questions = 20;         //Don't bother using const int, just use int.  Since these are all 20, make it one variable.
	const int test_answers = 20;           
	char answers = 20;                     //This is not defined correctly.  Delete this line.
	int number_of_correct_answers;

	char answers[test_answers] = { 'B', 'D', 'A', 'A',
		'C', 'A', 'B', 
		'A', 'C', 'D', 'B', 'C', 'D', 
		'A', 'D', 'C', 'C', 'B', 'D', 'A'};
    char test_answers[test_questions];     //This name variable name is taken.  Change to student_answers or something.  
    //You need to definte count with => int count;
    int test_answers[count]; //this name is taken again.
    
    //Now you need to have the user enter in the student grades.  This should be done with a for loop.  
    cout << "what were the student's answers\n";
    cout << "type the answers with a space in between\n";
    test_answers[count] = toupper(test_answers[count]);
	cin >> test_answers[test_questions];
    
    //Now you need to compare student answers to the answer key.  You almost got it, but I would use an if statement inside the loop.
    for (count = 0; test_answers[count] == answers[count]; count++)
    number_of_correct_answers = number_of_correct_answers + 1;
    if (number_of_correct_answers = 11)                   //change to if(number_of_correct_answers >= 11)
    cout << "the student passed\n";
    else
    cout << "the student failed\n";
    //add system("pause"); here.  Otherwise application will run and shut down immediately after completion.
    return 0;
} }
I had some help in class but still didn't get it all right although I'm VERY close.
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
#include <iostream>
using namespace std;
const char test_questions = 20;
	int number_of_correct_answers;
   const int student = 20;
   const int test_answers =20;
   int counter = 0;
   char student_answers = 20;	

int main()
{

	char answers[test_answers] = { 'B', 'D', 'A', 'A',
		'C', 'A', 'B', 
		'A', 'C', 'D', 'B', 'C', 'D', 
		'A', 'D', 'C', 'C', 'B', 'D', 'A'};


char student_answers[student];

student_answers[student] = toupper(student_answers[student]); // if it wasn't for this line the program would run with the exception of a debug error at the end

while (counter <= 19)
{
cout << "what were the student's answers? ";
cout << "type the answers with a space in between\n";
cin >> student_answers[student];
counter++;
}
while (student_answers[counter] == answers[counter])
{
	number_of_correct_answers = number_of_correct_answers + 1;
counter++;
if (number_of_correct_answers >= 11)
cout << "the student passed\n";
else
cout << "the student failed\n";
return 0;
} 
}
I changed variable names to make things clearer.
[code]

1
1>  array program.cpp
1>l:\comp sci stuff\array program\array program.cpp(21): warning C4700: uninitialized local variable 'student_answers' used
1>  array program.vcxproj -> L:\comp sci stuff\array program\Debug\array program.exe
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

I'm getting that warning even though I did initialize that variable as you can see from the top of my code. This warning becomes an error as soon as I try to run it.
@buster24
most of your corrections I made except for a couple I didn't understand such as that I defined a variable incorrectly. I also kept the declarations and initializations bunch outside the main function because when I put them inside (at the top) I got more errors then when I had them outside.
Your error is that you declared char student_answers = 20; first, then re-declared it as char student_answers[student];.
when I get rid of char student_answers = 20; to match the way I have my other array I still get the warning with the debug error when it runs.
1
2
3
4
 1
1>  array program.cpp
1>l:\comp sci stuff\array program\array program.cpp(21): warning C4700: uninitialized local variable 'student_answers' used
1>  array program.vcxproj -> L:\comp sci stuff\array program\Debug\array program.exe
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
You cannot overload local variable names. For example you have
char student_answers = 20; and char student_answers[student];.. They have the same name which cause a conflict also the variable type is a char student_answers yet you gave it an int value. You can cout that variable and its not what you want.
Are they still overloaded even if I got rid of char student_answers = 20; ? If so, I don't see why because as far as initialization and declaration don't they match and if so how are different in the way that's causing the warning.

program:
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
#include <iostream>
using namespace std;
const char test_questions = 20;
	int number_of_correct_answers;
   const int student = 20;
   const int test_answers =20;
   int counter = 0;
   	

int main()
{

	char answers[test_answers] = { 'B', 'D', 'A', 'A',
		'C', 'A', 'B', 
		'A', 'C', 'D', 'B', 'C', 'D', 
		'A', 'D', 'C', 'C', 'B', 'D', 'A'};


char student_answers[student];

student_answers[student] = toupper(student_answers[student]);

while (counter <= 19)
{
cout << "what were the student's answers? ";
cout << "type the answers with a space in between\n";
cin >> student_answers[student];
counter++;
}
while (student_answers[counter] == answers[counter])
{
	number_of_correct_answers = number_of_correct_answers + 1;
counter++;
if (number_of_correct_answers >= 11)
cout << "the student passed\n";
else
cout << "the student failed\n";
return 0;
} 
}
Last edited on
Please guys? I don't mean to be frustrating. I know that compared to you guys I'm an extreme beginner. So much so that I have to get use to the way you guys explain certain things. If I was rude at all I'm very sorry. I just had a hard time understanding what you guys were trying to say. (that may also apply to the future too but I'll do my best to not have it be.)
This line declares an array of 20 characters.

char student_answers[student];

Each of the twenty chars has not yet been initialised so are junk values.

This line

student_answers[student] = toupper(student_answers[student]);

means the same as this

student_answers[20] = toupper(student_answers[20]);

student_answers is only 20 characters long.
The first character is student_answers[0]
The last character is student_answers[19]

Your line wants to convert the 21st character which is not in the array.

If you fix this problem like this

student_answers[19] = toupper(student_answers[19]);

its still a problem because you are using toupper on a junk value.

This line only affects one character in the array and not the whole array.






Last edited on
Topic archived. No new replies allowed.
Pages: 12