Variables? Intergers.

This is a discussion on Variables? Intergers. within the C++ Programming forums, part of the General Programming Boards category; Hi, i've been wondering if i have this program Code: #include <iostream.h> int main() { int you; cout<<"how are you?"; ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    15

    Variables? Intergers.

    Hi, i've been wondering if i have this program
    Code:
     #include <iostream.h>
    int
    main() 
    { 
      int you;
      cout<<"how are you?";
      cin>>you;
      cout<<"You are:"<<you;
      cin.get();
      cin.get();
      return 0; 
    }
    before u start saying how bad it is e.t.c if i want to store a couple letter what would it be? because an interger can only store numbers, what can store letters? Also were should i replace this?(i'm looking to replace "in you;" part of the code i think!)

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You can use arrays of char to act as a string of characters:
    Code:
     #include <iostream.h>
    int
    main() 
    { 
      char you[50];
      cout<<"how are you?";
      cin>>you;
      cout<<"You are:"<<you;
      cin.get();
      cin.get();
      return 0; 
    }
    For responses with more than one word, cin>> won't get it all. In that case you need cin.getline():
    Code:
     #include <iostream.h>
    int
    main() 
    { 
      char you[50]
      cout<<"how are you?";
      cin.getline(you, 50);
      cout<<"You are:"<<you;
      cin.get();
      cin.get();
      return 0; 
    }
    And if your compiler is new enough, C++ style strings are preferable to C style strings:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int
    main() 
    { 
      string you;
      cout<<"how are you?";
      getline(cin, you);
      cout<<"You are:"<<you;
      cin.get();
      cin.get();
      return 0; 
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    15
    Thanks alot it works!!! You are truly a god

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 10:07 PM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  5. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 03:46 PM

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