Welcome to Dream.In.Code
Getting C++ Help is Easy!

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!




Stuck on array/ for loop problem

 
Reply to this topicStart new topic

Stuck on array/ for loop problem

zerogee
post 21 Aug, 2008 - 08:33 PM
Post #1


New D.I.C Head

Group Icon
Joined: 20 Aug, 2008
Posts: 42

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];
    
    for(i=0; i < MAX_COUNT_STUDENT; i++)
    {
        {
         for(a=0; a < Student_Name; a++)
         ++a;
         cout << "Enter Student Name: ";
         --a;
         cin >> name[a];
        
        
         for (b=0; b < Exam_1_Grade; b++)
         ++b;
         cout << "Enter Exam_1_Grade: ";
         --b;
         cin >> exam_1[b];
        
        
         for(c=0; c < Exam_2_Grade; c++)
         ++c;
         cout << "Enter Exam_2_Grade: ";
         --c;
         cin >> exam_2[c];
        
        
         for(d=0; d < HomeWork_Avg; d++)
         ++d;
         cout << "Enter HomeWork_Avg: ";
         --d;
         cin >> HomeWork[d];
        
        
         for(e=0; e < Final_Exam_Grade; e++)
         ++e;
         cout << "Enter Final_Exam_Grade: ";
         --e;
         cin >> Final_Exam[e];
        
        }

    }

    return 0;
}

remember this is the input only. I have not started working on the output yet.
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 21 Aug, 2008 - 08:36 PM
Post #2


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,328



Thanked 57 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


QUOTE(zerogee @ 22 Aug, 2008 - 12:33 AM) *

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?

You've declared the array as type int.

CODE
int name[Student_Name];


You need to declare it as char or str in order to take alpha characters.
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 21 Aug, 2008 - 08:39 PM
Post #3


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,027



Thanked 173 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


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!" decap.gif
User is offlineProfile CardPM

Go to the top of the page

zerogee
post 21 Aug, 2008 - 09:10 PM
Post #4


New D.I.C Head

Group Icon
Joined: 20 Aug, 2008
Posts: 42

QUOTE(Martyr2 @ 21 Aug, 2008 - 09:39 PM) *

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!" decap.gif



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?
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 21 Aug, 2008 - 09:27 PM
Post #5


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,328



Thanked 57 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


QUOTE(zerogee @ 22 Aug, 2008 - 01:10 AM) *

any other suggestions?

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];

for(i=0; i < MAX_COUNT_STUDENT; i++) {
cout << "Enter Student Name: ";
cin >> name[i];
cout << "Enter Exam_1_Grade: ";
cin >> exam_1[i];
cout << "Enter Exam_2_Grade: ";
cin >> exam_2[i];
cout << "Enter HomeWork_Avg: ";
cin >> HomeWork[i];
cout << "Enter Final_Exam_Grade: ";
cin >> Final_Exam[i];
}

for(i=0; i < MAX_COUNT_STUDENT; i++) {
cout << "Student # ";
cout << i;
cout << " name : ";
cout << name[i];
cout << "Exam_1_Grade: ";
cout << exam_1[i];
cout << "Exam_2_Grade: ";
cout << exam_2[i];
cout << "HomeWork_AVG: ";
cout << HomeWork[i];
cout << "Final_Exam_Grade: ";
cout << Final_Exam[i];

}

return 0;
}


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.
User is offlineProfile CardPM

Go to the top of the page

barnwillyb
post 21 Aug, 2008 - 09:49 PM
Post #6


D.I.C Head

**
Joined: 22 May, 2007
Posts: 55


My Contributions


QUOTE(zerogee @ 21 Aug, 2008 - 10:10 PM) *

QUOTE(Martyr2 @ 21 Aug, 2008 - 09:39 PM) *

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!" decap.gif



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;

  for (int idx = 0; idx < STUDENTS_IN_CLASSROOM; idx++) {
      cout << student_name( idx ) << ": ";
          cin >> test_scores;
        // add student score to array
      load_student_scores( idx, test_scores );
  }
}
User is offlineProfile CardPM

Go to the top of the page

zerogee
post 22 Aug, 2008 - 05:46 AM
Post #7


New D.I.C Head

Group Icon
Joined: 20 Aug, 2008
Posts: 42

QUOTE(no2pencil @ 21 Aug, 2008 - 10:27 PM) *

QUOTE(zerogee @ 22 Aug, 2008 - 01:10 AM) *

any other suggestions?

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];

for(i=0; i < MAX_COUNT_STUDENT; i++) {
cout << "Enter Student Name: ";
cin >> name[i];
cout << "Enter Exam_1_Grade: ";
cin >> exam_1[i];
cout << "Enter Exam_2_Grade: ";
cin >> exam_2[i];
cout << "Enter HomeWork_Avg: ";
cin >> HomeWork[i];
cout << "Enter Final_Exam_Grade: ";
cin >> Final_Exam[i];
}

for(i=0; i < MAX_COUNT_STUDENT; i++) {
cout << "Student # ";
cout << i;
cout << " name : ";
cout << name[i];
cout << "Exam_1_Grade: ";
cout << exam_1[i];
cout << "Exam_2_Grade: ";
cout << exam_2[i];
cout << "HomeWork_AVG: ";
cout << HomeWork[i];
cout << "Final_Exam_Grade: ";
cout << Final_Exam[i];

}

return 0;
}


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.....smile.gif 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
User is offlineProfile CardPM

Go to the top of the page

zerogee
post 22 Aug, 2008 - 05:53 AM
Post #8


New D.I.C Head

Group Icon
Joined: 20 Aug, 2008
Posts: 42

QUOTE(barnwillyb @ 21 Aug, 2008 - 10:49 PM) *

QUOTE(zerogee @ 21 Aug, 2008 - 10:10 PM) *

QUOTE(Martyr2 @ 21 Aug, 2008 - 09:39 PM) *

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!" decap.gif



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;

  for (int idx = 0; idx < STUDENTS_IN_CLASSROOM; idx++) {
      cout << student_name( idx ) << ": ";
          cin >> test_scores;
        // add student score to array
      load_student_scores( idx, 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
User is offlineProfile CardPM

Go to the top of the page

Akelo
post 22 Aug, 2008 - 06:12 AM
Post #9


D.I.C Head

**
Joined: 12 Dec, 2007
Posts: 77


My Contributions


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


Attached File(s)
Attached File  CSCI1300_Notes_3__Control_Flow.htm ( 74.38k ) Number of downloads: 12
Attached File  CSCI1300_Notes_3__Control_Flow.htm ( 74.38k ) Number of downloads: 7
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 04:52AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month