Thread: Please tell me why it won't work.

  1. #1
    Registered User yodoblec's Avatar
    Join Date
    Jun 2003
    Posts
    27

    Please tell me why it won't work.

    #include <iostream.h>
    int main()
    {
    int astring[25];
    cout<<"What is your first name: ";
    cin>>astring[25];
    cout<<"Thank you. "<<astring[25];
    return 0;
    }

    I'd like to know your opinion. So please respond.

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    1.- your reading in your array and printing out wrong.

    2. you are declaring an array of integers- yet the value you are reading in is a string not an integer.
    Mr. C: Author and Instructor

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    First, don't use header files with .h extensions. Use the equivalent headers and read about the using directive here:

    http://www.cplusplus.com/doc/ansi/hfiles.html

    Second, your program doesn't work because your array is of type int and your asking the user to input their name. Also, after you declare the variable, you just use it's name, not the name with the dimension.

  4. #4
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	char astring[25];
    
    	cout << "What is your first name: ";
    	cin.getline ( astring, 25 );
    
    	cout << "Thank you. " << astring << endl;
    
    	return 0;
    }

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User yodoblec's Avatar
    Join Date
    Jun 2003
    Posts
    27
    Thanks guys I guess I didn't realize I put in an integer number.

    "Must of been high on something"

  7. #7
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    You did click on that link XSquared provided, right?

  8. #8
    Registered User yodoblec's Avatar
    Join Date
    Jun 2003
    Posts
    27
    Yeah, I just did. Sorry about not usin "
    Code:
    " "
    ". Even though I do know HTMl.........

    "Must of been high on something"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM