Join 132,360 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,261 people online right now. Registration is fast and FREE... Join Now!
Ok, I am starting on my next project which it to enetr students names, grades, and then give a final letter grade. But right now I an just working on the input data. The code i have written so far is to enter student 1 name, exam1 grade, exam2 grade, homework grade, and final exam grade and then cycle to the next student. I complied the input and didn't have any errors, but for the student name it onle lets me enter numbers. How can i code this to enter characters?????? This is the code i have so far.
CODE
#include <iostream> #include <string>
using namespace std;
int main() { int i = 0, a = 0, average = 0, b = 0, c = 0, d = 0, e = 0;
const int MAX_COUNT_STUDENT = 6; const int Student_Name = 1; const int Exam_1_Grade = 1; const int Exam_2_Grade = 1; const int HomeWork_Avg = 1; const int Final_Exam_Grade = 1;
int name[Student_Name]; int exam_1[Exam_1_Grade]; int exam_2[Exam_2_Grade]; int HomeWork[HomeWork_Avg]; int Final_Exam[Final_Exam_Grade];
You need to specify the a character array not an array of int. You can do this using char student_name[50];. This specifies a character array which can hold 50 characters.
Anywhere you need to enter a string, you need to use a character array. A string is an array of characters.
Hope that is what you needed. Enjoy!
"At DIC we be character array creating code ninjas... char coolness[] = "we be mad kewl!"
You need to specify the a character array not an array of int. You can do this using char student_name[50];. This specifies a character array which can hold 50 characters.
Anywhere you need to enter a string, you need to use a character array. A string is an array of characters.
Hope that is what you needed. Enjoy!
"At DIC we be character array creating code ninjas... char coolness[] = "we be mad kewl!"
It did not work. when i change the
CODE
int name[Student_Name];
to this one
CODE
char name[Student_Name];
it did not like it at all. i had errors all on that one line. I also tried changing this lne;
After looking over your code, it doesn't look like you have a very good grasp on how an array works.
cpp
#include <iostream> #include <string>
using namespace std;
int main() { int i=0, MAX_COUNT_STUDENT=6; char name[MAX_COUNT_STUDENT][256]; int exam_1[MAX_COUNT_STUDENT]; int exam_2[MAX_COUNT_STUDENT]; int HomeWork[MAX_COUNT_STUDENT]; int Final_Exam[MAX_COUNT_STUDENT];
You need to specify the a character array not an array of int. You can do this using char student_name[50];. This specifies a character array which can hold 50 characters.
Anywhere you need to enter a string, you need to use a character array. A string is an array of characters.
Hope that is what you needed. Enjoy!
"At DIC we be character array creating code ninjas... char coolness[] = "we be mad kewl!"
It did not work. when i change the
CODE
int name[Student_Name];
to this one
CODE
char name[Student_Name];
it did not like it at all. i had errors all on that one line. I also tried changing this lne;
CODE
const int Student_Name = 1;
to this line;
CODE
const char Student_Name = 1;
but it also did not work. any other suggestions?
This is how I would enter the student scores and then store them in an array so you can use that information in your other functions.
CODE
// NUMBER OF STUDENTS IN CLASSROOM const int STUDENTS_IN_CLASSROOM = 10;
// ARRAY TO HOLD STUDENT SCORES int Scores[STUDENTS_IN_CLASSROOM];
// LOAD TEST SCORES void load_student_scores( int i, float total ) { Scores[i] = total; }
// STUDENT NAMES char * student_name( int idx ) {
switch (idx) { case 0: return "Tom Adams"; break; case 1: return "Barry Sanders"; break; case 2: return "Terry Collins"; break; case 3: return "Jim Grant"; break; case 4: return "Hale Irwin"; break; case 5: return "Debbie Moffat"; break; case 6: return "William Patterson"; break; case 7: return "Jay Smith"; break; case 8: return "Cindy Simmons"; break; case 9: return "Glenn Meager"; break; default: return "No Name"; } }
// GET SCORES FROM USER void enter_student_scores() { double test_scores;
After looking over your code, it doesn't look like you have a very good grasp on how an array works.
cpp
#include <iostream> #include <string>
using namespace std;
int main() { int i=0, MAX_COUNT_STUDENT=6; char name[MAX_COUNT_STUDENT][256]; int exam_1[MAX_COUNT_STUDENT]; int exam_2[MAX_COUNT_STUDENT]; int HomeWork[MAX_COUNT_STUDENT]; int Final_Exam[MAX_COUNT_STUDENT];
If this doesn't make sense, I'll explain it a bit.
We'll have a look at the array Final_Exam. It's declared as type int & has a size of 6. So in summary, it would look like this :
Final_Exam[][][][][][\0];
The \0 signifies the end of the Array.
So when we run through our for loop, it will fill this array. On the 1st run through the loop, lets say we put in a 1 (because it's declared integer).
It will now look like this:
Final_Exam[1][][][][][\0];
On the 2nd run through, we put in a 2.
Final_Exam[1][2][][][][\0];
So if we want to call the 2nd array, we'd issue the following :
CODE
cout << Final_Exam[1];
& it would print a 2.
no2pencil, What you mean i don't have a firm grasp of how an array works..... just kidding. to say i have a vague understanding is a major overstatement. hahaha
any, i totally understand what you are saying and i completely understand the way you explained the array. thank you. but i do have one question, i see this all the time and i not sure i completely understand it. i++ or ++i
when i try to complie the code you sent i got the same 3 errors for each of these;
CODE
char name[MAX_COUNT_STUDENT][256]; int exam_1[MAX_COUNT_STUDENT]; int exam_2[MAX_COUNT_STUDENT]; int HomeWork[MAX_COUNT_STUDENT]; int Final_Exam[MAX_COUNT_STUDENT];
These were the errors; error C2057: expected constant expression error C2466: cannot allocate an array of constant size 0 error C2133: 'exam_2' : unknown size
You need to specify the a character array not an array of int. You can do this using char student_name[50];. This specifies a character array which can hold 50 characters.
Anywhere you need to enter a string, you need to use a character array. A string is an array of characters.
Hope that is what you needed. Enjoy!
"At DIC we be character array creating code ninjas... char coolness[] = "we be mad kewl!"
It did not work. when i change the
CODE
int name[Student_Name];
to this one
CODE
char name[Student_Name];
it did not like it at all. i had errors all on that one line. I also tried changing this lne;
CODE
const int Student_Name = 1;
to this line;
CODE
const char Student_Name = 1;
but it also did not work. any other suggestions?
This is how I would enter the student scores and then store them in an array so you can use that information in your other functions.
CODE
// NUMBER OF STUDENTS IN CLASSROOM const int STUDENTS_IN_CLASSROOM = 10;
// ARRAY TO HOLD STUDENT SCORES int Scores[STUDENTS_IN_CLASSROOM];
// LOAD TEST SCORES void load_student_scores( int i, float total ) { Scores[i] = total; }
// STUDENT NAMES char * student_name( int idx ) {
switch (idx) { case 0: return "Tom Adams"; break; case 1: return "Barry Sanders"; break; case 2: return "Terry Collins"; break; case 3: return "Jim Grant"; break; case 4: return "Hale Irwin"; break; case 5: return "Debbie Moffat"; break; case 6: return "William Patterson"; break; case 7: return "Jay Smith"; break; case 8: return "Cindy Simmons"; break; case 9: return "Glenn Meager"; break; default: return "No Name"; } }
// GET SCORES FROM USER void enter_student_scores() { double test_scores;
Well, just from first glance it seem you hard coded the names of the students. The name need to be user input. and the rest of the code i follow somewhat. i'm just not sure how i would write the code for user input of name
Zero, got a resource for you that might help. An old prof of mine, instead of making us buy a book went ahead and wrote lessons for the different parts of the C language. I know C is a tad different than C++, but C++ means a superset of C (or you can think of it as the next increment of C).
The file attached goes over basic flow control in a program, and this will help to explain the for(i=0,i<LIMIT, i++). Also, another document attached goes over variables, and has a pretty good section on arrays. Just takes a little bit of substitution to get it working with C++, but the ideas are the same.
Oh, and i++ is the same as ++i, and this is known as a increment. basically, it takes the current value of i, and adds 1 onto it. So if you had i=0, then wrote i++ and you printed i to the screen, it would print 1. If you did another i++, then i would print 2. The same with i-- or --i, except it's a decrement, so it subtracts 1 rather than adds it.
This post has been edited by Akelo: 22 Aug, 2008 - 06:16 AM