Page 2 of 4 FirstFirst 1234 LastLast
Results 11 to 20 of 32

Thread: C++

  1. #11
    Banned
    Join Date
    Oct 2001
    Posts
    263
    yeah man, this cplusplus.com tutorial is alot better than my TY21, but then again, the TYs suck anyway

  2. #12
    Junior Member
    Join Date
    Jul 2002
    Posts
    10
    Thinking in C++, www.bruceeckel.com

    Great stuff
    Byte Me

  3. #13
    Banned
    Join Date
    Oct 2001
    Posts
    263
    ok now i have main() calling a menu() when menu() [which contains a fancy switch] gets back an error return (in this case if it gets back '10') i need the main function to...... i guess restart...... how would i go about doing this? i can figure 3 ways, a goto (which im told is a REALLY bas idea), system("program") (this would jam up ram tho wouldnt it? and its kinda bad cause i cant rename the binary) and calling the main function again (i think its recursion)..... this last one is what i setteled on for now but i know that theres gotta be a better way cause then it would just hog tons and tons of ram wouldnt it? if it kept getting return codes of 10?

    also, how to i tell the input to only accept one character input..... like i press a number then it continues its routine without me having to hit enter?

  4. #14
    The Iceman Cometh
    Join Date
    Aug 2001
    Posts
    1,209
    If you want to clear the screen but yoiu don't want to add extra libraries to your program, there's a very simple solution to clear the screen in C++ (actually, I used to do this in C, but it works in C++ as well).

    Code:
    #include <iostream.h>
    
    #define clrscr "\033[H\033[J"
    
    void main()
    {
      cout << "Press any key to clear the screen...";
      cin.get();
    
      cout << clrscr << "The screen is now cleared."
    }
    Hope that helps.

    AJ

    EDIT: To fix your problem with main needing to restart, simply use a loop within main (a do while would be a good solution). Just set up the do-while to repeat as long as the error code is returned back from menu. Also, I wouldn't recommend trying to use recursion... you don't sound very familiar with the language and recursion's a more advanced topic which you're probably not ready for yet.

    Also, why do you want the user to not have to press enter? The advantage to waiting is that if they accidentally press the wrong key, they can go back and change it before hitting enter.

  5. #15
    Senior Member
    Join Date
    May 2002
    Posts
    344

    c++

    hey, i have a really basic question. i want to have a really stupid thing in my prgram, where the program asks the user to input his/her name. i use char, but char seems like it only is for 1 charchater, so if the persons name is lets say emily, it just say hello e, welcome to blah blah program and then it crashes. anyways how do i fix it? below is what my currrent program would look like.

    #include <iostream>
    using namespace std;

    int main (void)

    {

    char userName;

    cout << "Please insert your name" << endl;
    cin >> userName;

    cout << "Hello and welcome " <<userName<< " to this really lame program"
    }

    anyways, if i typed in emily as the user name it would say in the last line, hello and welcome e to this really lame program. how do i get it too say the whole name? i have been wondering about this for like over a month and i keep on forgetting to ask my teacher
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  6. #16
    The Iceman Cometh
    Join Date
    Aug 2001
    Posts
    1,209
    If you want to use more than one character, you have to use an array of characters (also known as a string).

    Code:
    #include <iostream> 
    using namespace std; 
    
    int main(void) 
    { 
       char userName[21]; // Lets you have up to 21 characters for a name.
    
       cout << "Please insert your name" << endl; 
       cin >> userName; 
    
       cout << "Hello and welcome " << userName << " to this really lame program" 
    }
    AJ

  7. #17
    Banned
    Join Date
    Oct 2001
    Posts
    263
    avdven > great there, i like it, i made it into this:

    void clear(){cout << cout << "\033[H\033[J";}

    insted. that way its its own little function to call at will....... (no normaly i dont code on one line)

    i dont see how the dowhile would work cause half of it would have to be inside the switch, wouldnt it?

    well im not very familiar with any language really but i remember recursion........ its the same function calling its self over and over until a function is done........ tho beyond that im lost i think...... am i wrong?

    explination on the userName[21]....... the [21] part adds a 22 character array for the name to be stored in. userName alone has only room for one character, this [21] adds another 21 blocks for the name

  8. #18
    The Iceman Cometh
    Join Date
    Aug 2001
    Posts
    1,209
    You're right that recursion involves a function calling itself, but you should only use recursion when a program specifically requires that recursion be used (it's very dangerous if executed incorrectly). That's why it's generally not taught until the second or third programming course at most universities.

    As for the do-while, I'm not sure how your program is set up... see if this sort of looks how you have it...

    Code:
    int menu();
    
    void main()
    {
       int errorCode;
    
       do
       {
          errorCode = menu();
       } while (errorCode == 10);
    }
    
    int menu()
    {
       ...
       switch(...)
       ...
       return errorCode;
    }
    AJ

    EDIT:

    explination on the userName[21] a bit....... the [21] part adds a 22 character array for the name to be stored in. userName alone has only room for one character, this [21] adds another 21 blocks for the name
    Not quite... but close. the userName[21] creates a username with up to 21 characters (which are located in positions 0-20 of the character array) Declaring a variable as just a char only allows a character, but the [] make the variable into an array of characters instead. Also, if you want, you can include string.h and use the string variable type instead. This header file will also give you more string manipulation functions/procedures.

  9. #19
    Junior Member
    Join Date
    Jul 2002
    Posts
    10
    Originally posted here by avdven
    If you want to use more than one character, you have to use an array of characters (also known as a string).

    Code:
    #include <iostream> 
    using namespace std; 
    
    int main(void) 
    { 
       char userName[21]; // Lets you have up to 21 characters for a name.
    
       cout << "Please insert your name" << endl; 
       cin >> userName; 
    
       cout << "Hello and welcome " << userName << " to this really lame program" 
    }
    AJ
    What if they supply more than 20 characters (null terminator!)?

    Easier and safer to use a string:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std; 
    
    int main() 
    { 
       string userName;
    
       cout << "Name? >" << flush;
       getline(cin, userName); // get everything until the newline
    
       cout << endl;
    
       cout << "Hello and welcome " << userName << " to this really lame program" << endl;
    }
    Byte Me

  10. #20
    Banned
    Join Date
    Oct 2001
    Posts
    263
    ok avdven, but i have a switch in the main statment that is used to call the menu function and i use the return value as the error code, not another varriable, sorry that was my bad i wasnt spacific

    also, how can i write a message under where the input is at? like to make it look like (in conjunction with the clear screen) the message is being displayed below where there putting input? did that make sence?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •