Problems with loops

This is a discussion on Problems with loops within the C++ Programming forums, part of the General Programming Boards category; I jsut started to learn C++, i alrady know Java and that really helped but i started making a little ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    3

    Exclamation Problems with loops

    I jsut started to learn C++, i alrady know Java and that really helped but i started making a little counting app and it prints out what look to be completly random numbers like this:

    http://img207.echo.cx/img207/9397/untitled3jy.png

    what am I doing wrong? Here is the code for the count segment of my app:

    Code:
      else if (cho=="COU" || cho=="cou" || cho=="Cou"){
           cout<<"Enter a number to count to: ";
           cin>> c_num;
           cin.ignore();
           do{       
           x=0;
           cout<<x;
           x++;
           }
           while(x != c_num);
      }

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    You probably want to print each number on a new line. I would change your do-while loop to this:
    Code:
    do{       
           x=0;
           cout << x <<end;
           x++;
    }  while(x < c_num);
    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Code:
    do{       
           x=0;  //Here  you are resetting x to zero with every loop iteration
           cout<<x;
           x++;
           } while(x < c_num);
    you should consider saving the value of x into another variable before entering into the loop.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Code:
    else if (cho=="COU" || cho=="cou" || cho=="Cou"){
           cout<<"Enter a number to count to: ";
           cin>> c_num;
           cin.ignore();
           do{       
           x=0;
           cout<<x;
           x++;
           }
           while(x != c_num);
      }
    well, the first thing I notice is your else if statement... you need to make sure you're not using C-style arrays. make sure you're declaring cho with the keyword string.

    also, you're initializing x to zero in the loop. you want to do that outside and before the loop.

    other than that, I don't see much wrong with your code. unless of course you have some other problems elsewhere in your code.

    edit: one more thing. what happens if the user enters -5 when you ask for a number to count up to?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    3
    Thanks I got it fixed.

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    3
    Quote Originally Posted by major_small
    edit: one more thing. what happens if the user enters -5 when you ask for a number to count up to?
    It counts and counts and counts it gets stuck in a loop. Though I am not shure how to get it to count to a neg or from a neg.

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Though I am not shure how to get it to count to a neg or from a neg.
    Well, it depends on what you want it to do...

    The way the program is structured now, you are starting at zero, so counting-up to a negative number isn't logical... But, you might actually be able to do it! Maybe you're not waiting long enough when "stuck in a loop."

    In the computer's memory, there is no special place for the minus sign. The minus sign is simply the highest (leftmost) bit. This is why you can hold an unsigned int that's twice as big as a signed int. You have one "extra" bit with an unsigned number.

    When you count high-enough, the integer "overflows" into the sign-bit and the number suddenly becomes negative! Assuming your system has 32-bit integers, it will overflow at about 2,147,438,647. (It does NOT overflow with a bunch of 9s, because it's a binary counter.)

    You could test the user's input with an if-statement. If c_num is less than zero, you could "reject" the user's input. Or, you could count-down (by using x--) if the number input is negative .

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    36
    If you're going to count "down" to a number, why would u want to change it to x--?

    I would suggest

    Code:
    if(c_num < 0 )
      c_num = Math.abs(c_num);
    Your still counting the same distance, just you aren't changing the rest of your loop, or creating an entire duplicate loop

    Of course thats a java command, not sure where it is in C++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct Pointer Problems. (Warning: Long Post.)
    By Phoenix940 in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 09:04 PM
  2. Problems with functions and for loops
    By slava in forum C Programming
    Replies: 1
    Last Post: 11-08-2008, 12:30 PM
  3. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  4. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 12:18 AM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03:28 PM

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