c++ function, few q's about my function

This is a discussion on c++ function, few q's about my function within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; int main () { string user; string pass; string cpass; string em1; string em2; ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    43

    c++ function, few q's about my function

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
        string user;
        string pass;
        string cpass;
        string em1;
        string em2;
        string age;
        
        cout<<"Username: ";
        cin>>user;
        cin.ignore();
        cout<<"Password: ";
        cin>>pass;
        cin.ignore();
        cout<<"Confirm Password: ";
        cin>>cpass;
        cin.ignore();
        cout<<"E-mail: ";
        cin>>em1;
        cin.ignore();
        cout<<"Confirm E-mail: ";
        cin>>em2;
        cin.ignore();
        cout<<"Age: ";
        cin>>age;
        cin.ignore();
        cout<<"Details"<<"\n"<<"Username: "<<user<<"\n"<<"Password: "<<pass<<"\n"
            <<"Confirm Password: "<<cpass<<"\n"<<"E-mail: "<<em1<<"\n"
            <<"Confirm E-mail: "<<em2<<"\n"<<"Age: "<<age<<"\n";
        
        cin.get();
    }
    This function works,
    but I need to fix a few things
    Question 1:
    It shows "username" then I enter my username
    and then it shows password, then confirm password, then email, confirm email, and finally age, how do I get these to show "all" at once?


    Question 2:
    How do I clear the text, that I put in? it shows all the stuff I've entered, then it displays Details blah blah and I see double of what I put in!


    Question 3:

    When I put in Mythic as my username
    it shows password properly next, and everything works,
    if I put Mythic Fr0st, it shows like this
    Username: Mythic Fr0st
    Password: Confirm Password:

    if I put Mythic
    its
    Username: Mythic
    Password:
    Confirm Password:

    how do I fix this
    Last edited by Dave_Sinkula; 01-16-2007 at 10:43 PM. Reason: Minimal assistance with the wrapping.

  2. #2
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by Mythic Fr0st

    This function works,
    but I need to fix a few things
    Question 1:
    It shows "username" then I enter my username
    and then it shows password, then confirm password, then email, confirm email, and finally age, how do I get these to show "all" at once?


    Question 2:
    How do I clear the text, that I put in? it shows all the stuff I've entered, then it displays Details blah blah and I see double of what I put in!


    Question 3:

    When I put in Mythic as my username
    it shows password properly next, and everything works,
    if I put Mythic Fr0st, it shows like this
    Username: Mythic Fr0st
    Password: Confirm Password:

    if I put Mythic
    its
    Username: Mythic
    Password:
    Confirm Password:

    how do I fix this
    Q1: If you're talking about interactive form type like in html forms, you can't. Well, not normally at least. In C, you can print anywhere in console with moveto() function (CMIIW). But there's no such function in C++. So if you want to do that, you have to make your own assembly code. for this

    Q2: I don't quite understand what you're saying. Sorry.

    Q3: Use cin.getline() rather than cin >>.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    43
    Question 2:
    How do I clear the text, that I put in? it shows all the stuff I've entered, then it displays Details blah blah and I see double of what I put in!
    When I enter my username, and fill out all the details, after the last part has been entered

    it then displays

    Details:
    Username YourUsername

    and all that, so it duplicates, because the above text hasn't been erased...

    Question 1:
    It shows "username" then I enter my username
    and then it shows password, then confirm password, then email, confirm email, and finally age, how do I get these to show "all" at once?
    all I mean, is show

    Username:
    Password:
    Confirm Password:
    Email:

    and then you type in your username, then go to password, then type password in etc..

    I've seen it done on ms dos games!

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Q1: no need for ASM to make a move to xy in console, and moveto() does not seem to be a standard function, if it was, it would be in C++ since pretty much the whole standard C library is available in C++ directly.

    Q2:I assume you mean clear the screen so that you can you can print the details on a fresh screens. You can pump out a couple new lines, not care about seeing it in double, or check out curses.

    Q3: g4j31a5 is right, the reason is that the << operator will only read until the first whitespace.

    Those games were probably using platform specific code, or a curses library.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    43
    Q2 is correct, I dont understand the other 2,

    cin>> I dont use that

    or do you mean for this

    cin>>name;

    cin.getline(name);

    ?

    OH and my friend tested my program, and it shuts down for him! but not for me! OMG!
    Last edited by Mythic Fr0st; 01-16-2007 at 11:29 PM.

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    www.google.com has tons of answers, check it out, it is the hot new thing.

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    43

    yees

    Yep, thats true, I also google, and find nothing, I find a whole lot of COMPLEX things SIMILAR to what I put in, but nothing helpful that I can see...

  8. #8
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    @Wraithan
    Yeah, moveto() function is in conio.h which is non standart. I used to use it alot back then when I was still playing with Turbo C

    @Mythic Fr0st
    Look at this link:
    http://www.cprogramming.com/tutorial/lesson9.html

    And search for cin.getline.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  9. #9
    Registered User
    Join Date
    Jan 2007
    Posts
    9
    If you want to clear your screen so you can display the user's info on a clean sheet you could use system("cls"); which has worked for me in the past when i made something similar.
    You should think about possibly making structures to hold the users information (though that is just my opinion because i will always love structs) and it makes ur code look nicer
    Code:
    cout<<"Username: ";
        cin>>user;
    <.V.S.>
    Code:
     cout<<"Username:";
    cin.getline(user.name);

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    1)
    all I mean, is show

    Username:
    Password:
    Confirm Password:
    Email:

    and then you type in your username, then go to password, then type password in etc..

    I've seen it done on ms dos games!
    You can't yet. It's covered in an advanced topic called "windows programming". It will probably take you at least a year of studying C++ until you are ready for windows programming. Even then, windows programming will be very difficult.

    If you don't want to put that much time in, I advise you to study a different language. For instance, with Java you can make GUI's relatively quickly(~6 mths).

    2)
    Question 2:
    How do I clear the text, that I put in? it shows all the stuff I've entered, then it displays Details blah blah and I see double of what I put in
    C++ doesn't have a way to accomplish that. However, you can use functions specific to your operating system to do that. See here for windows:

    http://www.adrianxw.dk/SoftwareSite/...Consoles1.html

    3)
    When I put in Mythic as my username
    it shows password properly next, and everything works,
    if I put Mythic Fr0st, it shows like this
    Username: Mythic Fr0st
    Password: Confirm Password:

    if I put Mythic
    its
    Username: Mythic
    Password:
    Confirm Password:

    how do I fix this
    You are seeing that problem because of the way cin>> works. First, this is what your line of input looks like to the compiler:

    Mythic<space>Frost<carriage return>

    The way cin>> works is that it reads from the input by first skipping any leading whitespace, and then it reads up to but not including the first whitespace it encounters. Whitespace is considered to be any space, tab, or carriage return(called a newline). The rest of the input remains for the next read operation. So when you type in:

    Mythic<space>Frost<carriage return>

    the current read operation reads in Mythic and stops. The following space, plus Frost, plus the carriage return after Frost remain for the next read opertion. When the next read operation executes, there is no need for cin>> to wait for user input since there is data already available to read. The next time cin>> is encountered in your program, once again it will not wait for user input, and it will read in Frost and stop. The next time cin>> is encountered it will read in the carriage return and stop. If cin>> is encountered again, all the available data will have been read, so cin>> will wait until the user enters more data.
    Last edited by 7stud; 01-17-2007 at 03:08 AM.

  11. #11
    Registered User
    Join Date
    Jan 2007
    Posts
    43

    hmm

    Thanks guys...

    I'll try

    I get errors with cin.getline(name); though

    how do I fix it, is it in the right context?

  12. #12
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Have you even gone to the link I told you about before?

    Here's the snippet from it.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      char string[256];                               // A nice long string
    
      cout<<"Please enter a long string: ";
      cin.getline ( string, 256, '\n' );              // Input goes into string
      cout<<"Your long string was: "<< string <<endl;
      cin.get();
    }
    Or here's the link for further knowledge about getline: http://cppreference.com/cppio/getline.html
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  13. #13
    Registered User
    Join Date
    Jan 2007
    Posts
    43

    yea

    yea I did look at the link, it just really confused me:P

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  4. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 07:44 PM

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