simple question

This is a discussion on simple question within the C++ Programming forums, part of the General Programming Boards category; I am a beginner... I just wrote the following code... [code] #include <stdio.h> #include <iostream.h> #include <string> main() { using ...

  1. #1

    Exclamation simple question

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

    #include <stdio.h>
    #include <iostream.h>
    #include <string>

    using namespace std;

    string name;

    //name = "Test";

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

    cout<< "Hello " << name;




    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

    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
    another nice solution :

    #include <stdio.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;
      return 0;
    Programming is a high logical enjoyable art for both programer and user !!

  4. #4

    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)


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

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

  5. #5
    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:


    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.


    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, 08:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

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