urgent :((

This is a discussion on urgent :(( within the C++ Programming forums, part of the General Programming Boards category; hello guys i am having a difficulty with this simple program. I am only a beginner so it is kinda ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    4

    urgent :((

    hello guys
    i am having a difficulty with this simple program. I am only a beginner so it is kinda hard for me to program this simple program:

    program is supposed to ask user name, print it and aks for 3 numbers and add them up before printing the result.
    the this is that when asked for name, if i input my first name and my family name, it does not work and bypasses the step of asking numbers:

    the code is:

    #include <iostream>

    int main ()

    {
    char type [50];
    int a, b, c,d;

    cout<<"Hello! What is your name?\n";
    cout<<"Enter your name.\n"
    cin>>type; // this is where the problem is..if there is a space between the 2 names, it does not work

    cout<< "hello"<<type<<"please enter 3 numbers.";
    cout<< "number 1\n";
    cin>>a;
    cout<< "number 2\n";
    cin>>b;
    cout<< "number 3\n";
    cin>>c;
    d=a+b+c;
    cout<< "The answer is"<<d;

    return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    8
    #include <iostream>
    #include <stdio.h> //allows use of gets();

    int main ()

    {
    char type [50];
    int a, b, c,d;

    cout<<"Hello! What is your name?\n";
    cout<<"Enter your name.\n"
    gets(type); //lets you input more than one word at a time

    cout<< "hello"<<type<<"please enter 3 numbers.";
    cout<< "number 1\n";
    cin>>a;
    cout<< "number 2\n";
    cin>>b;
    cout<< "number 3\n";
    cin>>c;
    d=a+b+c;
    cout<< "The answer is"<<d;

    return 0;
    }

    hope that works, i aint tested it.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    145

    Talking

    You need to use a getline to be able to have the user enter a name with a space in it. I wrote up a short buncha code for you, and it is tested so it works. I threw in some extra formatting that you may have not learned yet.

    Hope that helps!


    Kyoto Oshiro
    http://www.angelfire.com/realm2/horizon/files.html
    Horizon Games
    Attached Files Attached Files

  4. #4
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    or you can get the names seperatley
    Code:
    #include <iostream.h>
    
    int main () 
    
    { 
    char first [25]; 
    char last [25];
    int a, b, c,d; 
    
    cout<<"Hello! What is your name?\n"; 
    cout<<"Enter your first name.\n"; 
    cin>>first;
    cout<<"Ener your last name.\n";
    cin>>last;
    
    cout<< "hello "<<first<<' '<<last<<" please enter 3 numbers."; 
    cout<< "number 1\n"; 
    cin>>a; 
    cout<< "number 2\n"; 
    cin>>b; 
    cout<< "number 3\n"; 
    cin>>c; 
    d=a+b+c; 
    cout<< "The answer is"<<d; 
    
    return 0; 
    }
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    You don't need the indirection operator when using getline

    i.e. cin.getline(string, stringlength);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Urgent Help Needed In Writing Algorithm!!
    By Vikramnb in forum C++ Programming
    Replies: 1
    Last Post: 01-09-2009, 12:46 PM
  2. beginner plzz help urgent
    By sara101 in forum C Programming
    Replies: 11
    Last Post: 01-14-2007, 10:38 PM
  3. pls help me urgent..
    By intruder in forum C Programming
    Replies: 4
    Last Post: 01-13-2003, 04:41 AM
  4. Help Needed: Borland C++ 5.5 Installation - URGENT!
    By Linette in forum C++ Programming
    Replies: 12
    Last Post: 03-09-2002, 06:44 PM
  5. Help.... Urgent... Thanks!
    By weihann in forum C Programming
    Replies: 0
    Last Post: 02-27-2002, 10:15 PM

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