Thread: Trying to work with Cin Function

  1. #1
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86

    Trying to work with Cin Function

    I am a novice programmer, and I'm trying to understand the Cin Function..

    I know there's a lot of problems with this, but i just wanna play around and learn some stuff. I want this program to be able to output the corresponding number when a user inputs one of the three names.. how would that work?

    #include <conio.h>
    #include <stdio.h>
    #include <iostream.h>
    int main(void)
    {
    int Jon=20, Chewwy=25, Popeye=30;
    cout<<"Type a name and See the age ";
    cin>>Christian;
    cin>>Chewwy;
    cin>>Popeye;

    printf("Press a key to continue...");
    getch();
    return 0;
    }

    Any advice is highly needed.

  2. #2
    Unregistered
    Guest
    you could try using an enumerated type, double table look up, array of struct/classes, 3d array of strings, ...

  3. #3
    Registered User blackwyvern's Avatar
    Join Date
    Jan 2002
    Posts
    60
    This will work for you


    #include <fstream.h>
    #include <string.h>

    char response [10];


    int main()
    {

    cout << "Input the name of Randy, Alex or Jack to see thier age.\n";
    cin >> response;

    while(strstr(response,"Randy")!=NULL)
    {
    cout << response << " is 10 years old.\n";
    return 0;
    }
    while(strstr(response,"Alex")!=NULL)
    {
    cout << response << " is 12 years old.\n";
    return 0;
    }
    while(strstr(response,"Jack")!=NULL)
    {
    cout << response << " is 14 years old.\n";
    return 0;
    }

    return 0;
    }




    This is mainly hacked coding... I am a terrible programmer but I get things to work in a forceful way...

  4. #4
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86

    Program Works, but DOS CLOSES

    Thanks for your help, especially blackwyvern...

    Just one more question..how can i get the DOS Box to stay open when I press ENTER.

  5. #5
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    See your other thread here:http://www.cprogramming.com/cboard/s...&threadid=9424

    for the answer.

  6. #6
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86

    If it's obvious I'm sorry

    Good lookin' out Govt..could you gimme an example on how to use it, cuz i keep tryin', but it closes when i press enter..

    If you can, use the example from blackwyvern ..thanks

  7. #7
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Oh, I see (::smacks forehead::)

    Do a search on the boards for "clearing the buffer"

    cin leaves the newline from the enter in the buffer, so when it goes to the getch function, it skips it because getch reads from the buffer. If you wanted to practice some really sloppy coding but see what I mean, put another getch right below the one you have.

  8. #8
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86

    Okay..one more thing

    GOVT..you're the man, just one more thing..

    when i do use "getch();"..the compiler gives me an error message.

    "implicit declaration of function `int getchar(...)"

    how do i resolve this? Could i be using it wrong?

    I swear if it works after this, This will be the last question on KEEPING THE DAMN DOS WINDOW OPEN..

  9. #9
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Are you using your original code, blackwyvern's, or some mix? If it's a mix, post it. Also, what compiler are you on?

  10. #10
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    If you're on Dev-C++, check this out:

    http://www.cprogramming.com/cboard/s...&threadid=8935

  11. #11
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    Hey..I'm using Bloodshed Dev C++ Version 4.0
    Thanks for your help


    #include <fstream.h>
    #include <string.h>


    char response [10]; <****I'm not sure what this does****


    int main()
    {

    cout << "\n Input the name ABC, DEF, GHI to see their age.\n";
    cin >> response;

    while(strstr(response,"ABC")!=NULL)
    {
    cout << response << " is 20 years old.\n";
    return 0;
    }
    while(strstr(response,"DEF")!=NULL)
    {
    cout << response << " is 15 years old.\n";
    return 0;
    }
    while(strstr(response,"GHI")!=NULL)
    {
    cout << response << " is 8 years old.\n";
    return 0;
    }

    return0;
    }

  12. #12
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    1st of all - response[10] is an array - look up arrays
    2nf - you still haven't included anyway to pause the program at the end...

  13. #13
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    #include <fstream.h>
    #include <string.h>
    #include <conio.h> ****I've Included this

    char response [10];


    int main()
    {

    cout << "\n Input the name ABC, DEF, or GHI to see their age.\n";
    cin >> response;

    while(strstr(response,"ABC")!=NULL)
    {
    cout << response << " is 20 years old.\n";
    return 0;
    }
    while(strstr(response,"DEF")!=NULL)
    {
    cout << response << " is 15 years old.\n";
    return 0;
    }
    while(strstr(response,"GHI")!=NULL)
    {
    cout << response << " is 8 years old.\n";
    return 0;
    }

    getch();*****// BUt i still get the error message "Implicit
    //declaration of function 'int getchar(...)'
    return 0;
    }

  14. #14
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    Hey..I'm using Bloodshed Dev C++ Version 4.0
    Thanks for your help


    #include <fstream.h>
    #include <string.h>


    char response [10]; <****I'm not sure what this does****


    int main()
    {

    cout << "\n Input the name ABC, DEF, GHI to see their age.\n";
    cin >> response;

    while(strstr(response,"ABC")!=NULL)
    {
    cout << response << " is 20 years old.\n";
    return 0;
    }
    while(strstr(response,"DEF")!=NULL)
    {
    cout << response << " is 15 years old.\n";
    return 0;
    }
    while(strstr(response,"GHI")!=NULL)
    {
    cout << response << " is 8 years old.\n";
    return 0;
    }

    return0;
    }

  15. #15
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86

    I'm so retarded

    Thanks to everyone who helped..i figured it out ..

    I was putting getch(); at the end before return0;, but there were like 5 other return0; in my program which i figured had to have getch(); before them also...what a relief..thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM