Thread: Odd coding problems....

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    67

    Odd coding problems....

    I am currently using Dev-C++ 4. But I am getting some odd coding errors when using cout and cin, here is a example

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
          int name;
          cout<<"What is your name?";
          cin>>name;
          cout<<"Welcome to Underair" <<name;
          cout<<"How old are you" <<name;
          system("PAUSE");
          return 0;
    }
    When I execute it, where the inputs should be the only thing that shows up is "575" everytime. Can anyone help me with this???

  2. #2
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    try

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <string>
    
    int main()
    {
          string name;
          cout<<"What is your name?";
          cin>>name;
          cout<<"Welcome to Underair" <<name;
          int age;
          cout<<"How old are you";
          cin >> age;
          system("PAUSE");
          return 0;
    }

  3. #3
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    the datatype you are specifying for name is int
    int means integer, which is number
    your looking for char...

    try this:
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
          char name[256];
          cout<<"What is your name?";
          cin.getline(name,256,'\n');
          cout<<"Welcome to Underair" <<name;
          cout<<"How old are you" <<name;
          system("PAUSE");
          return 0;
    }
    The string name thing...In RoD's post, i've never seen that before, but hey, i'll take that.
    I've just always used a character array for names and such.
    Have fun!
    Last edited by civix; 02-16-2003 at 09:37 PM.
    .

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    When I execute it, where the inputs should be the only thing that shows up is "575" everytime. Can anyone help me with this???
    I am a little confused by your question. But are you entering a name such as John or are you entering it in numbers. An int can only hold numbers, and name is declared as an int.

    edit: beat.

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    67
    I forgot about that...havn't coded in c++ in a long time...but can someone tell me how the char thing works?

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    char object[100];

    this reserves memory for 100 chars, which is done using an array. you call each slot in the array such as object[0], object[1], etc. an array of 256 chars isn't really necessary for a name, kindof a high number. hope this helps.

  7. #7
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    i used a string in my fix.

  8. #8
    Registered User
    Join Date
    Aug 2002
    Posts
    67
    Okay, I think I get it so its like this
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
          int age;
          char name[50];
          cout<<"What is your name?";
          cin.getline(name,256,'\n');
          cout<<"Welcome to Underair" <<name;
          cout<<"How old are you" <<name;
          cin>>age;
          cout<<"So you are" <<age;
          system("PAUSE");
          return 0;
    }
    And the maximum letters they can have in their name is 50?
    Last edited by skyruler54; 02-16-2003 at 09:48 PM.

  9. #9
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    Originally posted by skyruler54
    Okay, I think I get it so its like this
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
          int age;
          char name[50];
          cout<<"What is your name?";
          cin.getline(name,256,'\n');
          cout<<"Welcome to Underair" <<name;
          cout<<"How old are you" <<name;
          cin>>age;
          cout<<"So you are" <<age;
          system("PAUSE");
          return 0;
    }
    And the maximum letters they can have in their name is 50?
    Yup, you got it, and since about 3 months ago, i've preferred cin.getline over cin>>. I dont know why, but i've been told cin.getline is better.
    .

  10. #10
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    waaaaaiiiiiit.
    Code:
    name[50];
    cin.getline(name,50,'\n');
    // the second thing in cin.getline is always the same as your array.
    .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer coding
    By princez90 in forum C Programming
    Replies: 2
    Last Post: 05-15-2008, 10:07 AM
  2. some odd computer problems
    By DavidP in forum Tech Board
    Replies: 14
    Last Post: 03-31-2008, 06:25 AM
  3. Examples Problems needed...
    By dcwang3 in forum C Programming
    Replies: 7
    Last Post: 02-14-2008, 09:13 PM
  4. Odd solution to an odd problem
    By Zeeshan in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 07-20-2002, 05:44 PM
  5. Coding Problems
    By RpiMatty in forum C++ Programming
    Replies: 12
    Last Post: 01-06-2002, 02:47 AM