Thread: loop problem

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    3

    loop problem

    hello all still pretty new to programming. if anyone can shed some light on my problem it would be greatly appreciated.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
       for(int i=0; i<100; i++)
              {
               const char students=100;
               char names[students];
               
               cout<<"please enter the students names"<<endl;
               cin>>names[students];
               }
       
        system("PAUSE");
        return EXIT_SUCCESS;
    when i run this code it prints out "please enter the students names like 4 times after the first initial print out. Thank you in advance

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Well, for starters you really shouldn't initailize (or for the most part, declare) anything in a loop. The way you have it set up, you'd just be inputting into names[100] everytime, which by the way is outside of the arrays range. The reason you use a for loop for these things is to use the count control as your changing index. Try this:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    const char STUDENTS = 100;
    
    int main(void) {
       string names[STUDENTS];
    
       for(int i = 0; i < STUDENTS; i++) {       
               cout <<"Please enter the Student " << i + 1 <<"'s name." << endl;
               getline(cin, names[i], '\n');
               }
       
        cin.get();
        return 0;
    }
    Anothing thing worth mentioning that I changed in the example is the use of the string datatype. Using a character array would only allow you to input one name, since each element is only a single character. As it seems, you want to input many names. You need to use a string for that, or a two dimensional array. I also used getline() rather than cin so you can accept spaces in your string.
    Last edited by SlyMaelstrom; 12-07-2005 at 08:27 PM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    3
    wow thank you a lot SlyMaelstrom i greatly appreciate the help and thank you for pointing out my mistakes. Totally forgot to initialize the element to 0 instead of 100 hehe.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Well, for starters you really shouldn't initailize (or for the most part, declare) anything in a loop.
    This is not correct. Declare variables as locally as possible. Only in loops that take up a large amount of time and that you want to optimize does that make sense to declare something outside of a block of code if it doesn't need to be.

    In this case, though, the names array should be declared outside the loop because you want to use the same array for each time through the loop instead of creating a new array each time.

    >> const char STUDENTS = 100;
    You should make that int instead of char. It is confusing to use char in that case but it just happens to work. Since 100 is meant as a number and not a character, declare it as an int and not a char.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Daved
    >> const char STUDENTS = 100;
    You should make that int instead of char. It is confusing to use char in that case but it just happens to work. Since 100 is meant as a number and not a character, declare it as an int and not a char.
    Whoops, good catch. Little typo there. I think I just copied from his and didn't even notice.

    As for limiting scope. Yes, there are needs for it, such as resetting variables in nested loops and what have you, but the point I was trying to make is the need to be careful on where you're declaring things.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  4. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM