Thread: hi im new to c++ and my loop wont work :(

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    118

    hi im new to c++ and my loop wont work :(

    this is my code
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {int a1, a2, a3, a4, a5; 
    int s1=0, s2=1, s3=1, s4=1, s5=1; 
    int r1;
    int ts=s1+s2+s3+s4+s5; 
          cin>>a1;
          cin.ignore();
          cin>>a2;
          cin.ignore();
          cin>>a3;
          cin.ignore();
          cin>>a4;
          cin.ignore();
          cin>>a5;
          cin.ignore();
      r1=s1*a1;
      do {  s1+1;}
       while (r1<ts);
     cout<<r1;
       cin.get();
                               }
    im not sure why it dosent work any info would be great thanks in advance
    Last edited by thestien; 10-01-2006 at 07:16 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work? Oh, and why do you use such undescriptive variable names?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    well it always returns 1 as the answer and s1 dosent increment?

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    i use shortest names i can i hate typing plus this is only a example program to help me learn

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    57
    And dont you think that you can use array for a more readable code?

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    do {  s1+1;} //this does nothing, try s1++;
       while (r1<ts); 
        /*loop condition should be related to the loop body.
        This either loops once or forever.*/
    Besides that, it would be good to know what purpose this code has.

    [edit]
    i use shortest names i can i hate typing plus this is only a example program to help me learn
    Great! Save a minute on typing and spend hours debugging/trying to understand the code a few hours/days/weeks later.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    ok ill try that. the pupose is to get a few loops nested together loop though each other

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    ok i tried s1++ but it still doesnt return a value for r1?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > do { s1+1;}
    > while (r1<ts);
    Since your loop doesn't change either r1 or ts, the loop will either not loop at all, or it will loop forever.

    You've written basically one of
    do { s1+1;}
    while (false);

    do { s1+1;}
    while (true);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    i see what you mean. how can i get the loop to depend on the changing of s1 to make r1>= ts? i mean if a1 was 1
    Last edited by thestien; 10-01-2006 at 07:31 AM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you say what you intend to accomplish. Without any idea what you are trying to do, it is impossible to help you along.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    i once made this program before and it worked but i reinstalled windows and forgot to save. the purpose is to balance numbers so they all equal higher than a total of them ok that sounds wrong and impossible... er ok say a1,a2,a3,a4,a5 all are 1 then r1=1 because s1=1 times a1=1 . but this is lower than the total of s1,s2,s3,s4,s5 witch=5 so then i want it to s1++ so its now 2 but thats still to low so s1++ 3 is still low up untill s1(which eventually = 5)s1*a1=r1=5
    i know its not very clear but it should at this stage just incease s1 until s1*a1=5 or higher this isnt complete yet as i need to get the first loop to work in the way i need then ill build the other loops around it. thank you for helping

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Sorry, but I still do not understand what you are trying to do.

    State clearly what you intend to accomplish. Avoid talking about the algorithm that you intend to use at this point. For example: I intend to find the largest of five numbers, a1, a2, a3, a4 and a5.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    ok i have 5 numbers these 5 numbers are all different.well call them (a)
    i then have another 5 numbers these start the same. and these (b)
    i want to multiply the first number of (a) by the first of (b).
    this will make a new set of 5 numbers (c).
    i want the total of all the number (b) to be less than or equal to the corrisponding (c) number
    i have thought of using arrays but im a novice and the way i interact with the arrys seem challanging i hope its clearer now and again thank you

  15. #15
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    an example of this might be
    [4 5 6 7 8] [4 5 6 7 8]
    [1 1 1 1 1]total=5 [2 1 1 1 1] total= 6
    [4 5 6 7 8] [8 5 6 7 8]

Popular pages Recent additions subscribe to a feed