help with the while loop

This is a discussion on help with the while loop within the C++ Programming forums, part of the General Programming Boards category; i would like it so that the first time the person goes thru the loops, it enters the input number ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    15

    help with the while loop

    i would like it so that the first time the person goes thru the loops, it enters the input number to item1, the second time to item2, the third time to item3... and so on. any ideas how to do this?

    Here is the code

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    main()
    {
    	const float SALESTAX = .0825;
    	float totalprice;
    	float totalpricewithouttax;
    	float item1;
    	float item2;
    	float item3;
    	float item4;
    	float item5;
    	string morethingstosell;
    
    	cout<<"Do you have an item you would like to sell? ";
    	cin>> morethingstosell;
    	
    	for (int i = 0;i < morethingstosell.length(); i++) {
    		morethingstosell[i] = toupper (morethingstosell[i]);
    	}
    
    	while (morethingstosell == "YES") {
    
    	cout<<"Enter the price of an item: ";
    	cin>>item1;
    	cin>>item2;
    	cin>>item3;
    	cin>>item4;
    	cin>>item5;
    	
    	cout<<"Do you have more items being sold? ";
    	
    	for (int i = 0;i < morethingstosell.length(); i++) {
    		morethingstosell[i] = toupper (morethingstosell[i]);
    	cout<<"Do you have more items being sold? ";
    
                	}
    	}
    	
    }

  2. #2
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    Here is a general concept. Firstly, use an integer array. Each time you loop, get the data, and increment your position.

    • Create integer array, e.g., int item[5];
    • Create integer for loop handling, e.g., int i = 0;
    • while (more to sell) {
    • Insert data into item[i]
    • Increment i, e.g., i++;

    Code wise for example sake:
    Code:
    int main() {
        int i = 0, item[5];
    
        while (more to sell) {
            cin >> item[i];
            i++;
    
            /* fifth time around */
            if (i > 4)
                break;
        }
    
        return 0;
    }
    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    15
    i have no idea what arrays are yet =) havent gotten that far in my book

  4. #4
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Ah,

    I apologize. Arrays are a series of elements (variables) of the same type placed consecutively in memory that can be individually referenced by adding an index to a unique name.

    That means that, for example, we can store 5 values of type int without having to declare 5 different variables each with a different identifier. Instead, using an array we can store 5 different values of the same type, int for example, with a unique identifier.
    Code:
    int item[5];
    
    Index Position
    item[0]  First
    item[1]  Second
    item[2]  Third
    item[3]  Fourth
    item[4]  Fifth
    These are numbered from 0 to 4 since in arrays the first index is always 0, independently of its length. I'll let you come across arrays on your own time; no rush. This was just a background of arrays and how they operate.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  5. #5
    fuh
    fuh is offline
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    Array Tutorial

    By the way, what book are you reading?
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    15
    Learn to Program with C++ by John Smiley

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, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-19-2005, 11:30 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

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