while loop - still need help

This is a discussion on while loop - still need help within the C++ Programming forums, part of the General Programming Boards category; I have been working on a problem to enter the first term then a second term.... then add all of ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    11

    Unhappy while loop - still need help

    I have been working on a problem to enter the first term then a second term.... then add all of the numbers

    for example..
    enter first term: 1
    enter second term: 5
    output
    1+2+3+4+5 = 14

    I was able to get that to work correctly...
    now I am trying to do the second half of the program...
    to output
    1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 + 5*5*5 = answer

    this is what i have so far... I think I have the right idea but something like a bracket in the wrong place or maybe too many places

    #include<iostream.h>
    #include<math.h>

    main()
    {
    int firstnum, secondnum, answer;
    int total = 0;
    int total2 = 0;
    cout<<"Please enter beginning term: ";
    cin>>firstnum;
    cout<<"Please enter ending term: ";
    cin>>secondnum;

    while(firstnum<=secondnum)
    {
    {
    cout<<firstnum<<" + ";
    total=(total+firstnum);

    firstnum++;

    }
    cout<<" = "<<total<<"\n";


    {
    cout<<firstnum<<" * " << firstnum<<" * "<<firstnum;
    total2 = (firstnum * firstnum * firstnum);
    firstnum++;
    }

    cout<<" = "<<total2<<"\n";
    }


    }

    when i compile I get an error.... expecting a bracket....

    suggestions would be appreciated.

    thank you

    marie10913@aol.com

  2. #2
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    >total2 = (firstnum * firstnum * firstnum);

    here you are reassigning firstnum cubed to the total... which does not make it a running total...

    try...

    Code:
    total2 += (firstnum * firstnum * firstnum);
    oh... and to help with your questioning, use code tags...

    so far as the brackets... seems okay since you have a correct number and sequence of brackets, but maybe your compiler doesn't like that you have blocks without a looping variable... what is the exact error?

    hth...
    hasafraggin shizigishin oppashigger...

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    38

    while loop (try this)

    Try this:

    int multotal,total;
    while (firstnum <= secondnum)
    {
    cout<<firstnum<<"*"<<firstnum<<"*"<<firstnum;
    if (firstnum < secondnum)
    {
    cout<<" + ";
    }
    multotal=firstnum*firstnum*firstnum;
    total=total+multotal;
    firstnum++;
    }
    cout<<" = "<<total;
    SilasP

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    11

    still having looping problems

    I am still trying to make a program that:
    Enter first term: 1
    Enter second term: 4
    Output:
    1 + 2 + 3+ 4 = answer
    1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 = answer

    This is the code that I have:
    #include<iostream.h>
    #include<math.h>

    main()
    {
    int firstnum, secondnum, answer;
    int total = 0;
    int total2 = 0;
    cout<<"Please enter beginning term: ";
    cin>>firstnum;
    cout<<"Please enter ending term: ";
    cin>>secondnum;

    while(firstnum<=secondnum)
    {
    {
    cout<<firstnum<<" + ";


    total=(total+firstnum);
    firstnum++;

    }
    cout<<" = "<<total<<"\n";


    {
    cout<<firstnum<<" * " << firstnum<<" * "<<firstnum;
    firstnum++;
    }
    total2+= (firstnum * firstnum * firstnum);
    cout<<" = "<<total2<<"\n";
    }


    }

    But instead when I run the program I get:
    Please enter beginning term: 1
    Please enter ending term: 4
    1 + = 1
    2 * 2 * 2 = 27
    3 + = 4
    4 * 4 * 4 = 152

    Any suggestions would be really appreciated.

    Thank You

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I didn't test this as I don't have access to a compiler at the moment, but it was paper debugged, so the logic should work.
    Code:
    void printSingle(int start, int end){
        int i = s, total = 0;
        while(n < end){
            cout<<n;
            if(n != end-1) cout<<"+";
            total += n++;
        }
        cout<<"="<<total<<endl;
    }
    
    void printCube(int start, int end){
        int cubeTotal = 0, total = 0;
        for(int i = start; i < e; ++i){
            for(int j = 0; j < 3; ++j){
                cout<<i;
                if(j != 2) cout<<"*";
                cubeTotal *= i;
            }
            total += cubeTotal;
            cout<<"+";
        }
        cout<<"="<<total<<endl;
    }
    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    20
    Hi Learnin,
    I hear that you can enclose pretty much any code in {}, but I am wondering why you put them there. I have never seen anything like this. If they are for documentation, a couple of //words might help. The{} confuse me, and maybe the compiler. I, beginner, usually write }//end of while -- to keep myself straight.

    Also, I'm pretty sure that your code won't get the right answer. If the user enters, say, 1-5, I suspect the ++ will change the 1 into a 2 by the time you get to the * part, and then to a 3 by the time you get back to the + part.
    Good luck, Al

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 11:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 03:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 01:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

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