Thread: What to do? Please help!

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    20

    Angry What to do? Please help!

    NEW UPDATE - I've got the code working now. Check the latest post. Thanks to everyone for your help. I'm sorry to have bothered you with my ignorance -_-'.

    Okay, small update...the new code I have is:

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int y = 0;
        int z = 0;
        for (int x = 0; x == 1; z++) {
              cout<< "Enter 0 to exit or 1 to continue.";
              cin>> y;
              if (y == 1) {
                     if (x == 0) {
                          x++;
                          }
                     else {
                          return x;
                          }
                     cout<< "You have repeated this process "<< z <<" times. \n";
                     }
              else {
                     return 0;
                     }
        }
        cin.get();
    }
    Now, this is a working version (one that compiles perfectly fine, that is). Mainly, there are two problems with this one:
    It doesn't display the line I want displayed (Enter 0 to exit it 1 to continue) and terminates no matter what I enter,
    and it exits no matter what I enter as a value.

    So, my questions would really be:

    1. What's causing the program to not display my line of text defined at cout<< ?

    2. If those compiler errors were fixed, would the resulting program run like I want it to?

    (and, if the answer to 2 is a no, and you're feeling generous: Why wouldn't it?)

    I know I might be asking a lot...but I have fiddled with this for over 4 hours and I'm getting really, really stressed here -_-'.

    Thank you so much for all your help!

    P.S.: in case anyone's wondering...I added the 'return 0' on the 'else' statement because I thought returning 0 to 'main' would end the program...was that just a totally stupid thought?
    Last edited by Athildur; 04-01-2007 at 04:16 PM. Reason: update in status

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    630
    You forgot to declare y (int y).
    At the end you have to return 0;

    Also I wonder why you want for loop since there are only two options (0 and 1).

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    You have to declare y and z by doing this
    Code:
    <Data type> y = <Some value>
    THe best way, since your program runs in an endless loop is to have
    Code:
    int x = 0;
    while(true)
    {
      cin >> x;
      if(x == 0)
         break;
      else if (x == 1)
         continue;
    }
    return 0;

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    31
    u could try
    Code:
    int op;
    switch ( op ) {
         case 0:
            break;
         case 1:
            continue; }

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    Edit: Check the first post for an updated status. This is how most of my programming experiences have fared, by the way...I solve one problem and get two in return -_-'.

    I2u: Thanks for your reply, I'll try that. And the compiler actually doesn't give me any error on the return 0 absence.
    But I get your point. I could leave the return 0 out of the loop because it doesn't get to that return 0 until the loop ends.

    As for using the 'for' loop: Erm...because I had no idea how to declare integers and so didn't have any idea how to give x it's standard value for the 'while' loop. Otherwise I would have used the 'while' loop. The 'for' loop allows me to declare x's starting value. Plus, I'm expecting to need the 'for' loop for my future program anyway, so I might as well start practising :-p.


    indigo0086: Thanks for your reply. I'll have to try that declaration of 'y' and 'z' .
    The block of code is nice, but I'd prefer sticking to my own block of code as much as I can because I know what everything does :P. Nevertheless, my thanks for your input :-D.

    jmajeremy: Thanks for the suggestion, but like I said before, I'd rather stick to code I actually get...because I have no idea how to implement any of my other tasks into that block of code...

    ----
    Update:
    Ok, so I've just added the following:
    int y = 0;
    int z = 0;

    after the { from int main() and before the 'for' loop.

    Now it compiles without problems.

    But, when I run it, it still doesnt give me the line I specified at cout<<
    And it still terminates after I enter any given value and press enter .

    Anyone have any idea?
    Last edited by Athildur; 04-01-2007 at 04:10 PM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Can you post your current code? The code above should never run a single time through the loop.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    UPDATE

    Oh my gosh, I am so sorry for bothering you people with my stupid problems.

    I fixed the problem.

    Thank you to indigo for giving me the int y = 0 and int z = 0 ideas .

    My current code, which is compiling without error and running the way I want is:

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int y = 0;
        int z = 0;
        for (int x = 0; x == 0; z++) {
              cout<< "Enter 0 to exit or 1 to continue.\n";
              cin>> y;
              if (y == 1) {
                     if (x == 0) {
                          // do nothing
                          }
                     else {
                          x++;
                          }
                     cout<< "You have repeated this process "<< z <<" times. \n";
                     }
              else {
                     return 0;
                     }
        }
        cin.get();
    }
    Like you said, Daved, it didnt run through the loop even once. I noticed that immediately once I looked at it again and I can't believe how stupid I was not to notice it in the first place.

    Thanks for the comment, though, Daved .

Popular pages Recent additions subscribe to a feed