Stops before the end of the code.

This is a discussion on Stops before the end of the code. within the C++ Programming forums, part of the General Programming Boards category; Ok I just started C++ yesterday. Im somewhat familiar of how it works as I know html, css and a ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    4

    Stops before the end of the code.

    Ok I just started C++ yesterday. Im somewhat familiar of how it works as I know html, css and a little bit of PHP.

    So yesterday all I did was read through all the articles I could find about C++ and I waited until today until really starting to learn. I went through the first begginer tutorial teaching you about variables, input and output(I though it was very well written and clear by the way).

    So I decided before I go onto a harder thing I will try making something with that I learned
    Know what it does is ask you this quest.

    How old are you: and you type in the answer here
    Ok so you are: Right here it says a number 8 higher than the one you entered.
    Is that how old you are?: Now you should type in no and itl say
    O sorry so how old are you?: and you type in your age
    O so your _? I must have misread it last time: and thats the end

    Now my problem is when it gets to the part where its suppores to say o sorry so how old are you the window just closes down.

    I had a few other problems but I fixed them. Also when I compile it it no errors come up. I'v been experimenting for about 30 minutes and can't find a solution.

    Heres my code
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int thisisanumber;
        cout<<"please enter  your age: ";
        cin>> thisisanumber;
        cin.ignore();
        cout<<"so your: "<<8+thisisanumber <<"\n";
        
        int no;
        cout<<"is that how old you are?: ";
        cin>> no;
        cin.ignore();
        cout<<no <<"your not " <<"\n";
        
        int real;
        cout<<"Sorry I must of misheard you. So what is your real age then?: ";
        cin>> real;
        cin.ignore();
        cout<<"O , ok so your"<< real <<"\n";
        cout<<"Well thanks for spending this time with me even though"<<"\n";
        cout<<"I am just a computer programm" <<"\n";
       }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,326
    If you type in no when the program expects an int, then it will cause the read to fail. Change the variable called no to a string, and #include <string>, then see if it works.

    Once you fix that, your window will probably close before you can see the last part. Add cin.get() to the very end so it will stay open until you hit enter.

  3. #3
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Fixed code:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int thisisanumber,real;
        char no[10];
        cout<<"Please enter your age: ";
        cin>> thisisanumber;
        cout<<"So you are "<<8+thisisanumber<<endl;
    
        cout<<"Is that how old you are?: ";
        cin>>no;
        if(no[0]=='n'||no[0]=='N')
        {
        cout<<"Your not "<<thisisanumber+8<<endl;
        cout<<"Sorry I must have misheard you. So what is your real age then?: ";
        cin>> real;
        cout<<"Oh , OK so you are "<< real <<endl;
        }
        else
        {
        cout<<"So I'm right, you are "<<thisisanumber+8<<endl;
        }
        cout<<"Well thanks for spending this time with me even though"<<endl;
        cout<<"I am just a computer program" <<endl;
        cin.get();
        return 0;
    }
    Yeah Daved was right that was your problem, I just made some other minor changes.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,326
    IMO, many of changes made by jmd15 are steps in the wrong direction. Declaring variables at the start of the function is worse C++ style than Elivmar's. The cin.ignore() calls weren't necessary, but they weren't harmful and will actually help if and when getline is used to read in text, so removing them is even more unnecessary. I would use a C++ string instead of a C style character array. The return 0 at the end isn't bad, but it isn't better either.

  5. #5
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Using return 0; is correct programming and should always be used. I don't see him using getline anywhere in the code so why use cin.ignore();?? Hmm....unnecessary yet we should leave them in anyways? Is that what you're saying? When are programmers supposed to make their code as long as possible? Why is declaring variables at the start of the program worse style? Who are you to judge programming styles? There are many styles and none of them are neither correct nor incorrect. Yes, using a string would be better but you have to include another header. You should always keep your include files to a minimum if you can. My improved code a step in the wrong direction? Hardly.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    It isn't required to return 0 at the end of main (it implicitly does so)

    And without ignore() a newline is left in the stream, so the program will terminate without waiting for enter to be pressed at the end.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,326
    You said that the code was "fixed". It is not. It presents one alternative solution and some other style changes that are IMO worse or equivalent. If you provide someone with "fixed" code, it should be better than their original code, not the same or worse.

    BTW, in C++, declaring variables only when you use them is more important than in C, because good C++ programming techniques include doing lots of initialization work in object constructors. Often times you will only need to use certain variables part of the time. If you wait to declare them until you need them, you save the wasted initialization time on those cases where they are not used. In C, I believe you used to have to declare variables at the tops of functions, which is why that style is ingrained in many C progammers heads.

    Check out C++ Coding Standards by Sutter and Alexandrescu. Item 18 (Declare variables as locally as possible) discusses this subject, but the entire book is worth the money or the trip to the library.

  8. #8
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    It is "fixed" because it actually works, just because I have a mixture of C and C++ style of programming doesn't mean my code isn't any good.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  9. #9
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by Daved
    BTW, in C++, declaring variables only when you use them is more important than in C, because good C++ programming techniques include doing lots of initialization work in object constructors. Often times you will only need to use certain variables part of the time. If you wait to declare them until you need them, you save the wasted initialization time on those cases where they are not used.
    But you are not talking about the file at hand. There is no initialization time in declaring primitive datatypes.

  10. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    Ok now you guys are starting to confuse me. I only just learned if statements.
    I made a code using if statements and I still get the same problem.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,326
    >> But you are not talking about the file at hand. There is no initialization time in declaring primitive datatypes.

    Which is why it is a style question in this case. Declaring variables only when you need them is better in the cases I mentioned, and being consistent means doing so even for primitive types is good style.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,326
    >> I made a code using if statements and I still get the same problem.

    Don't worry about the side discussion. Post your latest code with your current problem. Did you change your "no" int variable to a string? Did it work?

  13. #13
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    Its still not working.
    I made a few changes too it and Ive been experimenting with it a lot.

    Heres my current code

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int thisisanumber;
        cout<<"please enter  your age: ";
        cin>> thisisanumber;
        cin.ignore();
        cout<<"so your: "<<8+thisisanumber <<"\n";
        
        int theanswer;
        cout<<"is that how old you are?: ";
        cin>> theanswer;
        cin.ignore();
        cout<<theanswer <<"your not " <<"\n";
        
        
        int real;
        cout<<"Sorry I must of misheard you. So what is your real age then?: ";
        cin>> real;
        cin.ignore();
        cout<<"O , ok so your"<< real <<"\n";
        cout<<"Well thanks for spending this time with me even though"<<"\n";
        cout<<"I am just a computer programm" <<"\n";
        cin.get();
    }
    What do I have to change and to what?

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,326
    You have to change the variable called theanswer to a string instead of an int. A string is a series of characters, an int is a number. If your program user types in "no" to answer your question, the program will read in that string. Since it is reading it into the variable called theanswer, it fails because "no" is not a number.

    So change the variable theanswer to string instead of int. The only other change you need is to add #include <string> at the top of your code to use the string class.

  15. #15
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    Ok I know what your talking about now.
    I have use strings. Im not up to that yet but I think when I get there I'll be able to make the code.

    Thanks A lot
    -Elivmar ~(E)~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 08:13 AM
  2. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  5. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

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