Need some minor help here

This is a discussion on Need some minor help here within the C++ Programming forums, part of the General Programming Boards category; I havent looked into classes much, so I started to about an hour ago. I basically just made the tutorial ...

  1. #1
    Codigious FingerPrint's Avatar
    Join Date
    Mar 2006
    Posts
    60

    Need some minor help here

    I havent looked into classes much, so I started to about an hour ago. I basically just made the tutorial class, on this site, and then added in virus as like the processorspeed and stuff. So you could figure out how fast your processor is running and how many viruses are on your computer(obviously these are both set values and doesnt actually tell anything about your actual computer).

    Anyways, I added in a little menu just to combine loops, if's and the class and such. The class is working fine, I know because I ran it with just the "Welcome to the mainframe" part(youll see what I mean when you look at the code below), and it worked fine. So I added in another menu where you can login or leave. After you login with the correct information, you are sent to the "computer mainframe" which also works fine. Then you can select either leave, view processor speed, or check for viruses. After making you choice it does do what its supposed to do. But after you view the speed or see how many viruses there are, and press enter, the program closes.

    I tried all I could think of to get it to bring up the " Welcome to the computer mainframe" part, but I just cant figure it out. Here is the code for it:

    Code:
    // Class stuff
    
    void login(){
         
         char username[25];
         char password[10];
         int logged;
         
         
         cout << " Username: ";
         cin.get();
         cin.getline(username, 25);
         if(strcmp(username, "bob") == 0){
                             cout << " Password: ";
                             cin.getline(password, 10);
                             strcmp(password, "bob");
                             logged = 1;
                             cout << "\n";
                             }
         else{
              login();
            }
    }
    
    
    Computer compute;
    
    int main(){
        
        int begin;
        int opt;
        int logged;
        
        logged = 1;
        do{
        cout << "\n";
        cout << " 1. Login\n";
        cout << " 2. Exit\n";
        cin >> begin;
        if( begin == 1){
            login();
            }
        else if(begin == 2){
             exit(0);
             }
        }
        
        while(logged = 0);
        
        {
        cout << "\n\n";
        cout << " Welcome to the Computer Mainframe\n\n";
        cout << " 1. Check Processor speed\n";
        cout << " 2. Check for viruses\n";
        cout << " 3. Exit\n";
        cin >> opt;
        if(opt == 1){
             compute.setspeed(100);
             cout << " Processor speed is " << compute.readspeed() << ".";
             cin.ignore();
             cin.get();
             }
        else if(opt == 2){
             compute.numviruses(4);
             cout << " " << compute.viruses() << " Viruses found.\n";
             cin.ignore();
             cin.get();
             }
        else if(opt == 3){
             exit(0);
             }
        else{
             }
        }
    }
    EDIT: Lol, forgot the ending code tag the first time.

    Im sure there are ways to make the program smaller and what not, but I would like it if you would just focus on the problem at hand. Thanks for any help you provide.


    EDIT #2: Pffft! What are the odds? Lol, literally like a minute after I posted this I had an idea. The idea was to add a do...while loop around the "computer mainframe" part. Guess what, it works now. It may not be the best fix, but its simple, and I solved it on my own...which is a goo dthing to me. Here is all I did.

    Code:
    // Same as above code
    
    do{       // Added the DO here(obviously)
        cout << "\n\n";
        cout << " Welcome to the Computer Mainframe\n\n";
        cout << " 1. Check Processor speed\n";
        cout << " 2. Check for viruses\n";
        cout << " 3. Exit\n";
        cin >> opt;
        if(opt == 1){
             compute.setspeed(100);
             cout << " Processor speed is " << compute.readspeed() << ".";
             cin.ignore();
             cin.get();
             }
        else if(opt == 2){
             compute.numviruses(4);
             cout << " " << compute.viruses() << " Viruses found.\n";
             cin.ignore();
             cin.get();
             }
        else if(opt == 3){
             exit(0);
             }
        else{
             }
        }
        while(logged = 1);    // And added the WHILE here(obviously...again)
    }
    Last edited by FingerPrint; 10-06-2006 at 07:20 PM.
    Code:
    /* ------------------------------------------------------------------*/
                               // INSERT CODE HERE
    /* ------------------------------------------------------------------*/

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    make a boolean named run or somesuch, set it to true when the program starts then do while(run) { //your menu } and in your exit option, set run to false and then use continue; to skip to the top of the loop.

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    try commenting out the cin.ignore() 's and see if that helps w/ your program terminating before you even get a chance to see it.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Awesome FingerPrint... I too have posted a question on here only to realize how to solve it heh.

  5. #5
    Codigious FingerPrint's Avatar
    Join Date
    Mar 2006
    Posts
    60
    The brain, normally I dont include cin.ignore(), just cin.get(). But for some reason when I made this, when you selected "1. Check Processor Speed" it shows the processor speed and then brings the menu back up. I added the cin.ignore()'s so that it ignored the first pressing of enter to allow you to view the processor speed more easily. Its kind of confusing when the menu just pops up again because the single line the processor speed takes up blends in heh. But it still works either way.
    Code:
    /* ------------------------------------------------------------------*/
                               // INSERT CODE HERE
    /* ------------------------------------------------------------------*/

Popular pages Recent additions subscribe to a feed

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