Thread: simple question

  1. #1
    Unregistered
    Guest

    Exclamation simple question

    I am a beginner... I just wrote the following code...

    [code]
    #include <stdio.h>
    #include <iostream.h>
    #include <string>

    main()
    {
    using namespace std;

    string name;

    //name = "Test";

    cout << "Enter your name: ";
    cin >> name;

    cout<< "Hello " << name;

    getchar();
    getchar();
    getchar();


    }

    [code\]


    now, when i type in "m a"
    it says
    Hello m

    and the getchars work.

    when i type "mh a"
    it says
    Hello mh

    and the getchars work

    but when i type "m ah"

    it says
    Hello m

    and the getchars wont work..
    the program ends.

    why is this happening ?

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    3

    Arrow Answer

    Because the getchar() functions are not working at all

    cin stores all the characters enterd up until the first space, what is happening is your assigning all the characters enterd up until the first space into "name".... if you want to get 2 words you could do:

    string name, name2;

    cin >> name >> name2;
    cout << name << " " << name2;

    that would essentially print out both words (re-adding the space).

    The reason your getchar() functions dont work is because your not assigning the return value to a varible or printing it out...

    char c;

    c = getchar();
    printf( "%c", c );

    would print out one char that was enterd from the command line... all I can tell you is.. study up on your C fundamentals some more

  3. #3
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161
    another nice solution :

    Code:
    #include <stdio.h>
    #include<iostream.h>
    int main(int argc, char *argv[])
    {
      char name[30];                       // declaring array of charcaters
      cin.get(name,29);                   //get the name from the user 
      cout<<name<<endl;              //printing the name again 
      int i;
      cin>>i;
    
    
      return 0;
    }
    Programming is a high logical enjoyable art for both programer and user !!

  4. #4
    Unregistered
    Guest

    three more questions

    1) When I usually type getchar();
    it will take an 'Enter' key... I never assigned
    a variable for it before


    2) Suppose, I want people to type in
    3x4 or 3 4 or 3/4 or whatever... (2 numbers
    with a space or any other character)

    and it should store it in two seperate variables...

    i know how to scan it if it has an x or a ' ' in
    the middle... but what if it could be anything else ?

    scanf("%dx%d", &num1, &num2)

    also

    cin >> num1>>"x" >>num2


    3) What is cin.get(name,29); ?



    http://mahurshi.tripod.com/mainframes.htm

  5. #5
    Unregistered
    Guest
    3/4 is a valid string. however each char is a char, not a digit or whatever. Therefore you can do no calculations as is with that value. One common way to deal with fractions is to write your own class to handle them.

    I think getline() is a better solution than get() personally, but so be it.

    In C++ input and output are routinely handled using stream classes as opposed to printf() and scanf() functions from C, although the latter functions still work in C++. IF you list iostream (or iostream.h depending on your compiler) as an included file, then you are including the istream and ostream classes, and an predefined instance of each class, cin and cout.

    cin has a number of methods and operators associated with it (so does cout but that's another story). >> is the operator available with cin. get() and getline() are two of the commonly used methods (there are others like ignore(), good(), bad(), fail(), etc.). To use the methods the function call must be preceded by the name of the object calling the method, like this:

    cin.get()
    or
    cin.getline()

    Then, like other functions (methods are functions declared within a class), there are a number of arguments that are usually passed to the functions. get() and getline() each have three arguments: the buffer (aka char/array) to hold the string is listed first, the maximum number of char to be read into the buffer is listed second, and the terminating char is listed third.

    get() can default all three arguments when used with a single char variable as input, although this form is seldom used. It can take a single char as an argument. It can take a string as an argument, in which case you need to use the maximum number of char to read in, too. If you are using a string as the argument, the newline is the default char, but you could use any char that is valid in the character set of your compiler if you wish.

    getline() has to have the buffer and the maximum number of char to read in. It will only read into a string. It also has newline as the default terminating char, but any valid char can be substituted.

    Therefore:

    cin.get(name, 29);

    means read up to 29 char into the char array called name. If you find a newline char or EOF char before that point terminate input. Place a terminating null at the end of char input into name. (name must have at least 30 elements for this to work!!!) Leave the terminating char in the input stream buffer.

    cin.getline(name, 29);

    means the same thing as above, _except_ that getline() removes the terminating char from the input stream buffer, which can be extremely useful....and frustrating both.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM