while loop help

This is a discussion on while loop help within the C++ Programming forums, part of the General Programming Boards category; hey guys i got a big prob, this program works...sorta but im having troubles getting this loop to work right, ...

  1. #1
    Fallen AndyBomstad's Avatar
    Join Date
    Jan 2005
    Posts
    52

    Exclamation while loop help

    hey guys i got a big prob, this program works...sorta but im having troubles getting this loop to work right, i suck at while loops so i need help, heres the code
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    void autocheck();
    
    void main()
    {
        int sellerID;
        int autoID;
        double price;
        int c;
        ofstream fileout;
        
        while(c != 'y'){
        cout<<"Please Enter Seller ID : ";
        cin>>sellerID;
        cin.get();
        system("cls");
        cout<<"Please Enter Auto ID : ";
        cin>>autoID;
        cin.get();
        cout<<"Pease Enter Price : $";
        cin>>price;
        cin.get();
        
        fileout.open("C:/sellerID.txt",ios::app);
        fileout<<sellerID<<" ";
        fileout.close();
        fileout.open("C:/autoID.txt",ios::app);
        fileout<<"Seller's ID# "<<sellerID<<endl;
        fileout<<"Auto ID# "<<autoID<<endl;
        fileout<<"Price Sold "<<price<<endl<<endl;
        cout<<"Would You Like To Make Another Entry? y/n : ";
        cin>>c;}
    
        
    }
    the problem is at the end, when i try and loop it so that the user can enter more "data", when i go to enter 'y' so that it can start over the program crashes, im almost posative that its the loop, but anyways i really need help on this one guys, thanks!

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    1. STOP USING VOID MAIN
    2. Indent
    3. You need to initialize c to some value
    Code:
    c = 'y';
    4. You logic for while loop is messed up, try this:
    Code:
    while(c == 'y'){
    5. You need to use cin.get() here:
    Code:
    c = cin.get();
    cin.ignore(80, '\n');
    Now it works.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

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