Stuck in C++

Discussion in 'Computing, Science, and Technology' started by ZunePod, Jan 3, 2009.

  1. ZunePod

    ZunePod Banned

    Joined:
    Aug 28, 2008
    Messages:
    1,340
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    Iv'e finally deccided to learn a proper coding language.

    I followed this tutorial (Scroll to the bottom)

    Anyways, here is my code:

    Code:
    #include <iostream>
    
    void main() {
    	int iAge;
    	std::cout << "How old are you?" << std::endl;
    	std::cin >> iAge;
    	std::cout << "You are " << iAge << " years old." << std::endl;
    }
    
    now look at the attached file, what is going wrong, it just crashes after the input.

    Attached Files:

  2. lszanto

    lszanto New Member

    Joined:
    Nov 8, 2008
    Messages:
    74
    Likes Received:
    0
    Device:
    2G iPod touch
    I'm not 100% sure but from the looks of it I would say you can't get input and put it into an integer as it would return the number e.g 10 as "10" which is not an integer but rather 2 chars/a string. Does that make sense?
  3. mattym

    mattym New Member

    Joined:
    Sep 22, 2007
    Messages:
    786
    Likes Received:
    3
    Device:
    iPhone 4 (Black)
    not 100% sure were your stuff when wrong, but just a pointer.
    Instead of std::..... everytime
    under #include <iostream>

    add this line
    using namespace std;

    that will make it so you can just type stuff like.
    int iAge; or
    cout << "....";


    also you absolutly need that "return 0" at the end, before the last "}" in the main. That is what keeps the console window open. Its the command that gives you that "press any key to continue" option.

    Also that file you sent out did not work. I think you may ahve saved it wrong. You need to save it as a win32 console application.
  4. dabomb1022

    dabomb1022 Banned

    Joined:
    Feb 10, 2008
    Messages:
    1,288
    Likes Received:
    34
    Device:
    2G iPod touch
    Forgot
    return 0;
  5. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    Either return an int or remove void.
  6. jlgm25

    jlgm25 Well-Known Member

    Joined:
    Nov 1, 2008
    Messages:
    3,302
    Likes Received:
    28
    Device:
    4G iPod touch
    getch();
    return 0;

    then close

    Please Register or Log in to view images

  7. wibble

    wibble New Member

    Joined:
    Mar 16, 2008
    Messages:
    176
    Likes Received:
    0
    int main is standard. Suggest that you use it.
    std::cin.get() but seriously, you've already figured this out.
    Don't bump.

Share This Page