Repitition Structure

This is a discussion on Repitition Structure within the C++ Programming forums, part of the General Programming Boards category; I am having a problem to do the coding for the following: A repetition structure that will prompt the user ...

  1. #1
    JJ1
    Guest

    Repitition Structure

    I am having a problem to do the coding for the following:
    A repetition structure that will prompt the user for a value, read that value into the variable price, increment the variable total by the value entered, and continue repeating the process until total is greater than 10.00 or until the user enters 0 as a value to indicate that they are finished entering values.

    Would anyone be willing to help me out a little. This is what I come up with so far:

    #include <iostream.h>
    int main()
    {
    for(int x=0;x<10;x++)
    {
    cout<<x<<endl;
    }

    return 0;
    }

  2. #2
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    hmm.. whos this gues here.. posted a code from some old file to get things done.. keep it up..



    this is how i would do it
    Code:
    # include <iostream.h>
    
    
    int main()
    {
    
    double num=-1;
    double total=0;
    
    while(num!=0 && total<10)
    {
    cout<<"\nEnter a number > ";
    cin>>num;
    
    total=total+num;
    
    }
    
    
    return 0;
    }

  3. #3
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Work off this:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	double userEntry = 1;
    	double totalEntry = 0;
    
    	while(totalEntry < 10.0 || userEntry != 0)
    	{
    		cout << "Please enter a number: ";
    		cin >> userEntry;
    		totalEntry += userEntry;
    	}
    	cout << endl << "Your total entry was: " << totalEntry << "." << endl;
    	return 0;
    }
    --edit--
    Dough! Foiled by vasanth, atleast when I used code tags, I indented, lol.
    Last edited by stumon; 08-01-2003 at 08:28 AM.
    The keyboard is the standard device used to cause computer errors!

  4. #4
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Originally posted by stumon
    Work off this:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	double userEntry = 1;
    	double totalEntry = 0;
    
    	while(totalEntry < 10.0 || userEntry != 0)
    	{
    		cout << "Please enter a number: ";
    		cin >> userEntry;
    		totalEntry += userEntry;
    	}
    	cout << endl << "Your total entry was: " << totalEntry << "." << endl;
    	return 0;
    }

    Your program is completely wrong.. Looks like you are confused with the || (or) stuff in the while loop.. your program would run into an infinite loop... the or has to be substituted by and &&.. though in normal english it may sound logical.. in programming it has a different meaning.. the first condition is always checked and if it is true the next condition is never checked...
    Last edited by vasanth; 08-01-2003 at 08:32 AM.

  5. #5
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    >>the first condition is always checked and if it is true the next condition is never checked...

    a.k.a. "short circuit logic"
    I would have figured that out if i ran the program. lol. It was something for him to work off of.
    The keyboard is the standard device used to cause computer errors!

  6. #6
    Registered User codegirl's Avatar
    Join Date
    Jun 2003
    Posts
    76
    Your program is completely wrong.. Looks like you are confused with the || (or) stuff in the while loop.. your program would run into an infinite loop... the or has to be substituted by and &&.. though in normal english it may sound logical.. in programming it has a different meaning.. the first condition is always checked and if it is true the next condition is never checked...
    A good way to think of it (or at least the way I think of it), is that when you do A && B ("A and B"), the loop or if statement only executes if A and B are both true. With A || B ("A or B"), the loop/if only executes if either A or B is true (or if both are true!).
    My programs don't have bugs, they just develop random features.

  7. #7
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Ok then, lets just get the confusing and/or situation out the way, use a for() loop, just like he started with.
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	double userEntry = 1;
    	double totalEntry = 0;
    	int i = 1;
    
    	for (i = 0; totalEntry < 10.0; i++)
    	{
    		cout << "Please enter a number: ";
    		cin >> userEntry;
    		totalEntry += userEntry;
    		if (userEntry == 0.0)
    			break;
    	}
    	cout << endl << "Your total entry was: " << totalEntry << "." << endl;
    	return 0;
    }
    The keyboard is the standard device used to cause computer errors!

  8. #8
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Originally posted by codegirl
    A good way to think of it (or at least the way I think of it), is that when you do A && B ("A and B"), the loop or if statement only executes if A and B are both true. With A || B ("A or B"), the loop/if only executes if either A or B is true (or if both are true!).
    well thats right.. but the problem usually arrises when you use negating conditions like


    a!=7 || b!=3


    something like that.... Here it is a bit more problematic for a newbie to understand...

  9. #9
    JJ1
    Guest

    Thanks to all for your help!

    Thanks for your help. I am very new to C++ and is working hard to understand.

    Thanks a lot.

    JJ1

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Replies: 5
    Last Post: 02-14-2006, 08:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 04:48 PM

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