int char and float

This is a discussion on int char and float within the C++ Programming forums, part of the General Programming Boards category; lets say i had a program saying Code: #include <iostream> using namespace std; int main() { float name; float lastname; ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    42

    int char and float

    lets say i had a program saying
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    float name;
    float lastname;
    cout<<"Enter first name"<<endl;
    cin>>name;
    cout<<"Enter last name"<<endl;
    cin>>lastname;
    cout<<"your first name is "<<name<<" and your second name is "<<lastname<<endl;
    
    cin.get();
    return 0;
    }
    why dosent that work?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Try using
    char name[100];

    or
    string name;

    You can't store "fred" in a float!!!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    if you use strings (ie)
    Code:
    string foo
    dont forget to include string.
    Code:
    #include <string>
    Strings are very cool - you can concatenate them easily using +, and they resize themselves. Very awesome.
    Ph33r the sphericalCUBE

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    42
    ok i kind of get what you mean can someone please convert my code to how it should be so i can work it out from that

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    51
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
    	std::string name;
    	std::string lastname;
    
    	cout << "Enter first name" << endl;
    	cin >> name;
    
    	cout << "Enter last name" << endl;
    	cin >> lastname;
    
    	cout << "your first name is " << name << " and your second name is " << lastname << endl;
    
    	cin.get();
    
    	return 0;
    }

  6. #6
    Registered User
    Join Date
    Nov 2003
    Posts
    42
    thanx so much you have just taught me how to use strings

  7. #7
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    remember to put in
    Code:
    using std::cout;
    using std::cin;
    using std::endl;
    Better than using using namespace std;
    Ph33r the sphericalCUBE

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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