Problem with some code

This is a discussion on Problem with some code within the C++ Programming forums, part of the General Programming Boards category; This may be some really stupid small problem, but I wrote a piece of code that would store passwords. It ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    26

    Problem with some code

    This may be some really stupid small problem, but I wrote a piece of code that would store passwords. It is really simply and not entirely done but I was wondering what the problem is with it. On running the code there is a space and then displays "Press ENTER to continue"
    Then it quits. And here it is:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int password;
        int input;
        int second_number;
        password = 908;
        std::string email;
        std::string steam;
        std::string WoW;
        std::string Mac;
        email = "abcd1234";
        steam = "abcd1234";
        WoW = "abcd1234";
        Mac = "abcd1234";
    
     do {
    	cout<<"Please enter the password: ";
    	cin>> input;
    	cin.ignore();
       switch ( input ) {
         case 908:
           cout<<"Acsses Granted.\n";
           cout<<"\n";
           cout<<"1.Email Password.\n";
           cout<<"2.Steam Password.\n";
           cout<<"3.WoW Password.\n";
           cout<<"4.Mac Password.\n";
           cout<<"Input the password you would desire: ";
           cin>> second_number;
           cin.ignore();
          switch ( second_number ) {
             case 1:
               cout<<"\n";
               cout<<"The Email Password is "<< email <<"\n";
               break;
             case 2:
               cout<<"\n";
               cout<<"The Steam Password is "<< steam <<"\n";
               break;
             case 3:
               cout<<"\n";
               cout<<"The WoW Password is "<< WoW <<"\n";
               break;
             case 4:
               cout<<"\n";
               cout<<"The Mac Password is "<< Mac <<"\n";
               break;
             case 23:
               cout<<"\n";
               cout<<"Password Managment acssesd.";
                break;
    
             default:
               cout<<"Incorrect Selection";
               break;
          }
    
         default:
           cout<<"Acsses Denied.\n";
           break;
        }
     } while ( input != 908 );
       cin.get();
    }
    Also I was wondering how do you create memory for the program. Such as while in the program you could modify the passwords so that the passwords werent in the code exactly i think but they were in a file elsewhere which you could modify in the program through and option.

    Thanks in advace.
    Last edited by A13W; 02-10-2008 at 05:50 PM. Reason: Updated Code.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    the program runs fine for me. make sure your input is a number (ie only chracters 0-9). before i read the entire code i was going to guess it was a problem with not having cin.ignore(), but you have that so... not sure why it wouldnt run for you as it seems to fine for me, again. verify the code youve posted and the code your trying to compile and run are equivalent (ie are you compiling the wrong file? etc)

    "how to create memory for the program"? do you mean dynamic memory? if so look up new and delete keywords. you then go on to talk about files, so you probably just mean files not dynamic memory. for files (and dynamic memory), please see the faq as this is why it exists.

    hope it helps

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    Could it be a problem with my compiler? I use codeblocks and i copied the code from the thread and it still dosent work. Same thing as said in im original post.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    code::blocks is an ide, not a compiler. however it probably installed something like MinGW for its compiler. i doubt its an issue with the compiler. only thing i recommend is, again, verify your input number, verify the file you are running and compiling, or try and run the compiled .exe via the command prompt, not through the IDE.

    hope someone else can provide you the solution

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    Thank you so much. It dosent seem to run in the ide but clicking on the exe makes it work. And dynamic memory, the makes memory that is permenent until you delete it? It was not clear on the FAQ.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    yes dynamic memory is allocating a size of memory that is unknown at compile time and can change during the execution of the program. it stays allocated until properly deallocated with the delete keyword or until the machine is restarted. if you cant find sufficient information about it in the faq, search around (google is likely your best source) for it.

  7. #7
    NotSoAvgProgrammer
    Join Date
    Jul 2007
    Location
    Virginia, U.S.
    Posts
    57
    Make sure that you rebuilt the program, and that it confirmed it down below in the little compiler notes box.
    A quality job is not necessarily a *good* job. A quality job is, by definition, conformance to requirements. Therefore a *good* job, may not be a quality job.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you type something non-numeric, that will do two things: set failbit on the input stream, and leave all those characters in the input stream. By default, ignore only ignores one character at a time, and I think it needs to the input stream to be good to work. Anyway, try adding the following lines in the place indicated:
    Code:
    	cout<<"Please enter the password: ";
    	cin>> input;
    	cin.clear();
    	cin.ignore(80, '\n');
    The clear resets the failbit, and the ignore will ignore up to 80 characters or up to a carriage return, whichever comes first. (You can put other things in 80; if you look around, you'll have seen people recommend numeric_limits<int> or something like that.)

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    Thank you. I have one last question though if you could answer it. If you are getting annoyed then just ignore it but when you said that dynamic memory is deallocated when you machine restarts does that mean if you store passwords in there they would disapearl on reboot and if yes then how could you permanently store passwords that could be modified in the program not through the code but through and option.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    Quote Originally Posted by nadroj View Post
    yes dynamic memory is allocating a size of memory that is unknown at compile time and can change during the execution of the program. it stays allocated until properly deallocated with the delete keyword or until the machine is restarted.
    Correction:
    Until the program terminates. The OS will (most likely) clean it up.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    yes, on most moderm/decent OSs they will clean it up, otherwise it would be there until RAM is cleared ie computer restarted.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by A13W View Post
    Thank you. I have one last question though if you could answer it. If you are getting annoyed then just ignore it but when you said that dynamic memory is deallocated when you machine restarts does that mean if you store passwords in there they would disapearl on reboot and if yes then how could you permanently store passwords that could be modified in the program not through the code but through and option.
    Then you would need to store them in a file. Files are forever (barring disk failures/formats).

  13. #13
    coder
    Join Date
    Feb 2008
    Posts
    127
    Quote Originally Posted by A13W
    Also I was wondering how do you create memory for the program. Such as while in the program you could modify the passwords so that the passwords werent in the code exactly i think but they were in a file elsewhere which you could modify in the program through and option.
    Infact your question was so clear since the first post...
    Don't care about dinamic memory at the moment, you just need to learn how to handle file's input/output

    http://www.cprogramming.com/tutorial/lesson10.html
    http://www.cplusplus.com/reference/iostream/fstream/
    Last edited by carlorfeo; 02-10-2008 at 04:52 PM.

  14. #14
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    Thank you very much everyone for the help. It is very much appreciated.

  15. #15
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    How do you put a value in for a string in the code, what is the format?
    Like
    Code:
    int a;
    int a = 5
    but for strings.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem
    By sybariticak47 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2006, 11:50 AM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  3. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 02:05 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2003, 12:53 AM

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