Problem with std::cin

This is a discussion on Problem with std::cin within the C++ Programming forums, part of the General Programming Boards category; The following code compiles just fine: Code: #include <iostream> #include <stdlib.h> #include <wincon.h> using namespace std; class course { public: ...

  1. #1
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309

    Problem with std::cin

    The following code compiles just fine:
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <wincon.h>
    using namespace std;
    
    class course
    {
        public:
            int nHour;
            char cGrade;
            int nMaxPoints;
            int nCurrentPoints;
            char CourseName[255];
    };
    
    course x[25];
    
    int main();
    
    //function to add a new a course
    void addcourse()
    {
        int a;
        a = 0;
    
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 
        FOREGROUND_GREEN | FOREGROUND_INTENSITY);
        
        std::cout<<"\nPlease enter the course name:";
        std::cin>>x[a].CourseName[255];
        std::cout<<"\nPlease enter the hour you have this class:";
        std::cin>>x[a].nHour;
        
        a++;
        std::cout<<"\n";
        system("PAUSE");
        main();
    }
    
    //function to read courses
    void readcourse()
    {
    
        int s;
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 
        FOREGROUND_RED);
        std::cout<<"\nEnter the course hour to find:";
        std::cin>>s;
        std::cout<<"Course Name:\n" << x[s].CourseName[255];
        std::cout<<"\nCourse Hour:\n" << x[s].nHour;
        system("PAUSE");
        main();
    }
    
    //main function to find out options
    int main()
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_BLUE);
        
                 std::cout<<"::::::::::                                           ::::::::::\n"
                 <<":::::::::: G   R   A   D   E   K   E   E   P   E   R ::::::::::\n"
                 <<"::::::::::                                           ::::::::::\n";
        
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE);
        
        std::cout<<"\n\n\nEnter '1' to make a new a course, '2' to read an existing course!";
        int choice;
        std::cin>>choice;
        int loop=0;
        while(loop==0)
        {
            if(choice==1)
                {
                    addcourse();
                    loop++;
                }
        
            if(choice==2)
                {
                    readcourse();
                    loop++;
                }
        }
        return 0;
        system("PAUSE");
    }
    However, when it reaches the bolded part of the function, it goes straight to the system("PAUSE"). Is there something wrong with the code that I'm not seeing? Or is it a problem somewhere else?
    To code is divine

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    change the line to:
    Code:
    std::cin>>x[a].CourseName;

  3. #3
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Forget what I said before editing it. I changed it to that, and it works for some reason, but why does it do it for the array and not the integer?


    EDIT2: Ok I changed the variable back and it works now......I still don't know what the problem was but thanks.
    Last edited by 7smurfs; 10-23-2004 at 04:26 PM.
    To code is divine

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    delete these lines:

    int main();
    main();

    because a program should only have one main() and it should never be called, only defined. Change this line:
    std::cin>>x[a].CourseName[255];


    //to this
    std::cin>>x[a].CourseName;

    because you only indicate the size of the array when you declare it, not when you use it.

    Also the system("PAUSE"); line after the return 0; in main() is wasted as it will never be seen. Put it before return 0;, or better yet use cin.get(); instead of system("PAUSE");
    Last edited by elad; 10-23-2004 at 04:34 PM.

  5. #5
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    delete these lines:

    int main();
    main();
    Theres a reason I have them there, so I can go back to the main function easily.

    If you can show me a better way, tell me please. So far this is the only way I can find to get back to the main() function. Also, there IS only one main. The first time I use it I declare it, so I can call it, the other two times I call it, and the final time is the acctual function. As far as I know this is the only way to go back to that function.

    Anyways, thanks for the help, I've finally managed to get it to work! Thanks for the help again!
    Last edited by 7smurfs; 10-23-2004 at 04:56 PM.
    To code is divine

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    If you can show me a better way, tell me please
    Remove the main() function calls from readcourse() and addcourse(). Then change the while loop in main to:

    Code:
        while(1)
        {
            if(choice==1)
                    addcourse();
            else if(choice==2)
                    readcourse();
            else
                    break;
    
        }

  7. #7
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Thanks, I just tried that and it works alot better than before! Thanks for both your help!
    To code is divine

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    No problem. You should never have to explicitly call the main function from anywhere in your code. Just make good use of loops, and you should be fine.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >You should never have to explicitly call the main function from anywhere in your code.
    You should never be able to either. The C++ standard doesn't allow recursive calls to main.
    My best code is written with the delete key.

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Prelude
    >You should never have to explicitly call the main function from anywhere in your code.
    You should never be able to either. The C++ standard doesn't allow recursive calls to main.
    does the standard explicitly disallow it, or is it just not define an end result?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    Quote Originally Posted by major_small
    does the standard explicitly disallow it, or is it just not define an end result?
    Disallows it. The reason is that the compiler may insert code into the beginning of main() for the constructors of variables you declare outside of main. The variables were declared globally before you declared main(), so they should be done first. If you recursively call main(), then you would be calling those constructors multiple times -- not good.

    EDIT: Sory, I forgot to cite my source:
    http://coding.derkeiler.com/Archive/...4-03/4100.html

    The data above was not mine
    I'm not that smart.
    Last edited by Kybo_Ren; 10-23-2004 at 06:22 PM.

  12. #12
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Kybo_Ren
    Disallows it. The reason is that the compiler may insert code into the beginning of main() for the constructors of variables you declare outside of main. The variables were declared globally before you declared main(), so they should be done first. If you recursively call main(), then you would be calling those constructors multiple times -- not good.

    EDIT: Sory, I forgot to cite my source:
    http://coding.derkeiler.com/Archive/...4-03/4100.html

    The data above was not mine
    I'm not that smart.
    I know the obvious pitfalls that would cause everybody to tell the VB n00bs not to do it, but I'm wondering if there's actually a clause in the standard that says that you can't do that...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  13. #13
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Ok I just tried the loop out...is there anyway I can get it so it starts over at main() instead of just looping through the function over and over again?


    edit: I've tried placing main() after the readcourse() call, but that just causes it to go crazy, and from what I've read it's not a good idea to do so..
    Last edited by 7smurfs; 10-23-2004 at 06:45 PM.
    To code is divine

  14. #14
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    why would you need that?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  15. #15
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    It's supposed to be a gradekeeping program that lets you add courses, and then check the courses you have. To go from adding a course to checking the exisiting courses it I need to be able to get back to main.
    To code is divine

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. cin problem
    By mikahell in forum C++ Programming
    Replies: 12
    Last Post: 08-22-2006, 11:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21