1

Im working on a program that will prompt the user for their name & etc and voltage and current values to calculate power. It will then store all the data into a csv file.The problem is, my program goes into an infinite loop when the user inputs a character instead of integer or float for slot,lux,lux1,a(voltage),b(current)...How can I avoid this ?

//the program terminates when user enter -100 for voltage
#include <iostream>
#include <fstream>
#include<string>
#include <iomanip>
using namespace std;

    int main()
    {
        float a=0,b=0,c=0;
        int i,no=0,slot,lux=0,lux1=0;
        string name,id,date,time,lot,shift;
        ifstream indata;
        ofstream outdata;

        system("color 0A");

        cout<<"\n";
        cout<<"Enter Name:";
        cin>>name;
        cout<<"Enter Employee Number:";
        cin>>id;
        cout<<"Enter Date:";
        cin>>date;
        cout<<"Enter Time:";
        cin>>time;
        cout<<"Enter Lot No:";
        cin>>lot;
        cout<<"Enter Shift:";
        cin>>shift;
        cout<<"Enter Slot:";
        cin>>slot;//when User enter a character here,it goes into infinite loop



        cout<<"\n";
        cout<<"REMINDER:\n";
        cout<<"\n";
        cout<<"Center Cavity Lux Range is : 10500--10600\n";
        cout<<"Cavity 1 Lux Range is : 9500--9600\n";
        cout<<"\n";
        cout<<"Enter Center Cavity Lux Value:";
        cin>>lux;//when User enter a character here,it goes into infinite loop

        while((lux<10500)||(lux>10600)){
        cout<<"Incorrect Value.Please Enter the correct LUX value:";
        cin>>lux;
        }

        cout<<"Enter Cavity 1 Lux Range:";
        cin>>lux1;

        while((lux1<9500)||(lux1>9600)){
        cout<<"Incorrect Value.Please Enter the correct LUX value:";
        cin>>lux1;
        }


        outdata.open("Out.csv", ios::app);

        outdata<<"Solar Panel Test"<< endl;
        outdata<<"\n"<< endl;
        outdata<<"Name:"<<","<<name<< endl;
        outdata<<"Employee Number:"<<","<<id<< endl;
        outdata<<"Date:"<<","<<date<< endl;
        outdata<<"Time:"<<","<<time<< endl;
        outdata<<"Lot No:"<<","<<lot<< endl;
        outdata<<"Shift:"<<","<<shift<< endl;
        outdata<<"\n"<< endl;
        outdata<<"Center Cavity Lux Value:"<<","<<lux<< endl;
        outdata<<"Cavity 1 Lux Range:"<<","<<lux1<< endl;

        outdata<<","<<","<<","<< endl;
        outdata<<","<<","<<","<< endl;
        outdata << "No,Slot,Voltage(V),Current(mA),Power(VmA)" << endl;

        cout<<"\n";
        cout<<"Program Starts!\n";
        cout<<"\n";

        while (!(a ==-100))
     {

        cout<<"Enter VOLTAGE:";
        cin>>a;   //when User enter a character here,it goes into infinite loop

        while(((a<0)||(a>=5))&&(a!=-100))
        {
        cout<<"Incorrect Value.Please Enter the VOLTAGE :";
        cin>>a;
        }

        cout<<"Enter CURRENT:";
        cin>>b;  //when User enter a character here,it goes into infinite loop

        while((b<0)||(b>=17)){
        cout<<"Incorrect Value.Please Enter the CURRENT :";
        cin>>b;
        }

        c=a*b;

        if((a>0)&&(a<5))
         no++;



        if(c<=25.4)
        {
          cout<<"\n";
          cout<<"|----   |---|     -----   |      \n";
          cout<<"|----  |-----|      |     |      \n";
          cout<<"|     |       |     |     |      \n";
          cout<<"|    |         |  -----   |____  \n";
          cout<<"\n\n\n";
        }
          else 
        {
          cout<<"\n";
          cout<<"|----    |---|    |----  |----     \n";       
          cout<<"|    |  |-----|   |____  |____     \n";
          cout<<"|----  |       |       |      |    \n";
          cout<<"|     |         |  ____|  ____|    \n";
          cout<<"\n\n\n";

        }


          outdata<<no<<","<<slot<<","<<a<<","<<b<<","<<c<< endl;  

     }
        indata.open("Out.csv");

        system("pause");
        return 0;
    }

I tried using this code for slot,lux,lux1,a(voltage),b(current):

while (!(cin >> slot))
    {
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(),'\n');
    cout << "Please input a proper number for slot: " << endl;
    }

But now with this, I have to press each and every value of mine twice instead of once. Pls do help me !

4
  • Is this your code at all? I think this is what it means to be. When a user enter some invalid values, it is supposed to give an alert and let the user input again. Commented Apr 29, 2014 at 2:39
  • @YuchenZhong: Yes, but it doesn't consume the invalid input, which results in the infinite loop. Commented Apr 29, 2014 at 2:43
  • @YuchenZhong : Ok..so how do I solve this in codes ? Commented Apr 29, 2014 at 2:44
  • @YuchenZhong : It is my code...exactly ! when the user enter invalid values like character,I want to prompt the user until he/she enters a valid integer or float.so how do I solve this in codes ? Commented Apr 29, 2014 at 3:05

3 Answers 3

2

Using cin >> is generally only for "toy" programs. If you want to read user input in a "real world" way, you need to use getline() and convert/parse the string to whatever data type you want checking for validity all the while, i.e., have special functions that parse/check for integers, dates, times, etc.

Sign up to request clarification or add additional context in comments.

Comments

2

you need to clear the error state of the input but also delete the bad input (it was detected as wrong and activated the error flag but is still waiting there)

you need to add

    if (cin.fail()){
        cin.clear();
        cin.ignore(100,'\n'); // it will ignore 100 characters or get to the end of the line.
    }

at the start of every WHILE loop that need to get only numbers.

This is staying with the general line of your code but others suggested here more advanced and safe ways to do it.

Comments

0

Functions like isalpha, isdigit and others in cctype are useful. However, as another answer states, you can use getline(). However, I think it would be nice to have an alternative.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.