Link to home
Start Free TrialLog in
Avatar of 141ANG
141ANGFlag for United States of America

asked on

C++ Equation returning wrong value

It appears that my code is returning 45 regardless of what I type can anyone help me find where I went wrong?
//============================================================================
// Name        : GASCHARGE.cpp
// Author      : Kristopher Roy
// Version     :
// Copyright   : 
// Description : Gas Charges
//============================================================================
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <istream>
#include <fstream>
#include <cstdlib>
using namespace std; 
vector<int> operands;
vector<string> actionOperator; 
int main(int argc, char *argv[]) 
{
	cout << "Enter Current Gas Tax Percentage   ";
    string TP;
	// read in the WHOLE line
	while(getline(cin, TP))
	{
		if(TP.empty())
		break;
		// make an istream object
		istringstream iss(TP);
		string str2 = "";
		// break the line up wherever there are spaces
		while(getline(iss, str2, ' '))
		cout << "You entered  " << TP << "%  ";
    }
    system("PAUSE");
	{
		string GK;
		GK = "1";
		cout << "Please type 1 for Leaded or 2 for Unleaded ";
		string GC;
		getline(cin, GC);
		cout << "You entered " << GC << "  ";
		system("PAUSE");
		{if (GK == GC) 
			{
				cout << "Leaded Gas ";
				cout << "Please enter current Leaded Gas cost per Gallon ";
				string LGC;
				getline(cin, LGC);
				// make an istream object
				istringstream iss(LGC);
				string str2 = "";
				// break the line up wherever there are spaces
				while(getline(iss, str2, ' '))
				cout << "You entered " <<"$" << LGC << " ";
				system ("PAUSE");
				{
					cout << "Please enter the gallons of gas pumped ";
					string LGP;
					getline (cin, LGP);		
					// make an istream object
					istringstream iss(LGP);
					string str2 = "";
					// break the line up wherever there are spaces
					while(getline(iss, str2, ' '))
					cout << "You entered " << LGP << " gallons pumped ";
					system ("PAUSE");
					{			
						string PK;
						PK = "1";
						cout << "Enter 1 for Self Pumped or 2 for Employee Pumped? ";
                        string PC;
						getline (cin, PC);
						cout << "You entered " << PC << "  ";
						system("Pause");
						if (PK == PC)
						{
							double dTP = 0.0;
							std::stringstream ss1(TP); 
							ss1>>dTP;
							double dLGC = 0.0;
							std::stringstream ss2(LGC);
							ss2>>dLGC;
							double dLGP = 0.0;
							std::stringstream ss3(LGP); 
							ss3>>dLGP;
							int P = 100;
							double result = ((((double)dTP)/P)*dLGC+dLGC)*dLGP;
							cout <<"The result is " "$" <<result<<endl;
						}
						else
						{
						string GNG;
						GNG = "1";
						cout << "Enter 1 for Gas-N-Go or 2 for Full-Service ";
                        string ES;
						getline (cin, ES);
						cout << "You entered " << ES << "  ";
						system("Pause");
						if (GNG == ES)
							{				
								cout << "Gas-N-Go Service ";
								cout << "Please enter current Gas-N-Go Service Charge ";
								string GNGSC;
								// GNGCS stands for Gas-N-GO Service Charge
								getline(cin, GNGSC);
								// make an istream object
								istringstream iss(GNGSC);
								string str2 = "";
								// break the line up wherever there are spaces
								while(getline(iss, str2, ' '))
								cout << "You entered " <<"$" << GNGSC << " ";
								system ("PAUSE");
								{
									double dTP = 0.0;
									std::stringstream ss1(TP); 
									ss1>>dTP;
									double dLGC = 0.0;
									std::stringstream ss2(LGC);
									ss2>>dLGC;
									double dLGP = 0.0;
									std::stringstream ss3(LGP); 
									ss3>>dLGP;
									double dGNGSC = 0.0;
									std::stringstream ss4(GNGSC); 
									ss3>>dGNGSC;
									int P = 100;
									double result = ((((double)dTP)/P)*dLGC+dLGC)*dLGP+dGNGSC;
									cout <<"The result is " "$" <<result<<endl;
								}
							}
						}
					} 
				}  
			}
				else
			{
				cout << "Unleaded ";
				system ("PAUSE"); 
			}
		}
	}	
	cout << " Task is complete."; 
	system("PAUSE");
}

Open in new window

Avatar of Infinity08
Infinity08
Flag of Belgium image

Take a look at your while loop in the beginning :

>>     string TP;
>>         // read in the WHOLE line
>>         while(getline(cin, TP))
>>         {
>>                 if(TP.empty())
>>                 break;
>>                 // make an istream object
>>                 istringstream iss(TP);
>>                 string str2 = "";
>>                 // break the line up wherever there are spaces
>>                 while(getline(iss, str2, ' '))
>>                 cout << "You entered  " << TP << "%  ";
>>     }

It will keep reading lines of input, until the user enters an empty line.
But, that also means that TP will be empty when the loop finishes. The value entered by the user will be gone.

Why do you need the loop ? Can't you do it without the loop ?
Avatar of 141ANG

ASKER

sure, I had just modified a line I foud on google, didn't actually know if that part was required or not. I will give it a try.
Avatar of 141ANG

ASKER

question if the person accidentally hits enter rather than typing something how do I loop it back to ask the querstion again without causing this problem?
Avatar of 141ANG

ASKER

also do I need this stuff for it to work?
istringstream iss(LGC);
string str2 = "";
// break the line up wherever there are spaces
while(getline(iss, str2, ' ')
Avatar of 141ANG

ASKER

ok here is what I did, now I am getting 48.825 every time, lol.
//============================================================================
// Name        : GASCHARGE.cpp
// Author      : Kristopher Roy
// Version     :
// Copyright   : 
// Description : Gas Charges
//============================================================================
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <istream>
#include <fstream>
#include <cstdlib>
using namespace std; 
vector<int> operands;
vector<string> actionOperator; 
int main(int argc, char *argv[]) 
{
	cout << "Enter Current Gas Tax Percentage   ";
    string TP;
	getline(cin, TP);
	cout << "You entered  " << TP << "%  ";
	system("PAUSE");
	{
		string GK;
		GK = "1";
		cout << "Please type 1 for Leaded or 2 for Unleaded ";
		string GC;
		getline(cin, GC);
		cout << "You entered " << GC << "  ";
		system("PAUSE");
		{if (GK == GC) 
			{
				cout << "Leaded Gas ";
				cout << "Please enter current Leaded Gas cost per Gallon ";
				string LGC;
				getline(cin, LGC);
				cout << "You entered " <<"$" << LGC << " ";
				system ("PAUSE");
				{
					cout << "Please enter the gallons of gas pumped ";
					string LGP;
					getline (cin, LGP);		
					cout << "You entered " << LGP << " gallons pumped ";
					system ("PAUSE");
					{			
						string PK;
						PK = "1";
						cout << "Enter 1 for Self Pumped or 2 for Employee Pumped? ";
                        string PC;
						getline (cin, PC);
						cout << "You entered " << PC << "  ";
						system("Pause");
						if (PK == PC)
						{
							double dTP = 0.0;
							std::stringstream ss1(TP); 
							ss1>>dTP;
							double dLGC = 0.0;
							std::stringstream ss2(LGC);
							ss2>>dLGC;
							double dLGP = 0.0;
							std::stringstream ss3(LGP); 
							ss3>>dLGP;
							int P = 100;
							double result = ((((double)dTP)/P)*dLGC+dLGC)*dLGP;
							cout <<"The result is " "$" <<result<<endl;
						}
						else
						{
						string GNG;
						GNG = "1";
						cout << "Enter 1 for Gas-N-Go or 2 for Full-Service ";
                        string ES;
						getline (cin, ES);
						cout << "You entered " << ES << "  ";
						system("Pause");
						if (GNG == ES)
							{				
								cout << "Gas-N-Go Service ";
								cout << "Please enter current Gas-N-Go Service Charge ";
								string GNGSC;
								// GNGCS stands for Gas-N-GO Service Charge
								getline(cin, GNGSC);
								cout << "You entered " <<"$" << GNGSC << " ";
								system ("PAUSE");
								{
									double dTP = 0.0;
									std::stringstream ss1(TP); 
									ss1>>dTP;
									double dLGC = 0.0;
									std::stringstream ss2(LGC);
									ss2>>dLGC;
									double dLGP = 0.0;
									std::stringstream ss3(LGP); 
									ss3>>dLGP;
									double dGNGSC = 0.0;
									std::stringstream ss4(GNGSC); 
									ss3>>dGNGSC;
									int P = 100;
									double result = ((((double)dTP)/P)*dLGC+dLGC)*dLGP+dGNGSC;
									cout <<"The result is " "$" <<result<<endl;
								}
							}
						}
					} 
				}  
			}
				else
			{
				cout << "Unleaded ";
				system ("PAUSE"); 
			}
		}
	}	
	cout << " Task is complete."; 
	system("PAUSE");
}

Open in new window

Avatar of aossdev
aossdev

While loop below will take care of the person who accidentally hits enter rather than typing something. You might add input validation so that user doesn't enter non-integer value.

                 
                 string str2 = "";

                 while(str2.size() == 0 )
                 {
                     getline(cin, str2);
                 }
                 cout << "You entered  " << str2 << "%  ";

You get '45' may be because somewhere user enters '-' [hyphen] instead of number.
>> question if the person accidentally hits enter rather than typing something how do I loop it back to ask the querstion again without causing this problem?

Then you can use a loop, but make sure that when you exit the loop, you keep the correct value.


>> also do I need this stuff for it to work?

You're never using str2, so no.
It was an attempt at sanitizing the input, but it wasn't really complete.

What you could do instead, is to try to extract a double value from the user's input. If you succeed, then store that double value, and continue with the rest of the application (that double value can then eventually be used in your equation). If no double value can be extracted from the user's input, then ask some new input, and try again.


>> ok here is what I did, now I am getting 48.825 every time, lol.

What input values did you give ?
Avatar of 141ANG

ASKER

so do I keep the original string line and add this like so
      cout << "Enter Current Gas Tax Percentage   ";
        string TP =  "";
        while(TP.size() == 0 )
       {  
           getline(cin, TP);
       }
        cout << "You entered  " << TP << "%  ";
      system("PAUSE");
Yes, this should work.
Avatar of 141ANG

ASKER

and this should retain the value to the end where it is used in the calculation?
Avatar of 141ANG

ASKER

that didn't work it just lets me hit enter infinitely.
In that case, make sure that you declare the variable  [string TP =  "";] way above in the code. For example right in the begining of main().
 


I think now it is not the while( ) loop which makes you hit enter infinitely. It is some other loop or code after the while loop segement.
Check that in debug mode with breakpoints.
Avatar of 141ANG

ASKER

still allows me to hit enter infinitely, I am guessing that I am missing the loop like Infinity08 suggested, but I am not sure how to add that. Also Infinity, I am totally clueless ass to how to add the doubles, do I use the same type of syntax that we used in the actual computation?
Avatar of 141ANG

ASKER

ok so after I hit enter with no info in it here are the resutls, hopefully this will help?

argc	1	int
+		argv	0x00244d58	char * *
-		TP	"" std::basic_string<char,std::char_traits<char>,std::allocator<char> >
-		[actual members]	{_Bx={...} _Mysize=0 _Myres=15 }	std::basic_string<char,std::char_traits<char>,std::allocator<char> >
+		std::_String_val<char,std::allocator<char> >	{_Alval={...} }	std::_String_val<char,std::allocator<char> >
+		_Bx	{_Buf=0x001ef8cc "" _Ptr=0xcccccc00 <Bad Ptr> }	std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Bxty
		_Mysize	0	unsigned int
		_Myres	15	unsigned int

Open in new window

Avatar of 141ANG

ASKER

ok, after watching the code in debug it does loop correctly, but I want it to loop all the way back to the prompt rather than just allowing you to type, does that mean that I need to move the while ?
This looks normal to me. And only says that you entered "" [null] in TP. After this control will go to while condition will find the TP size zero then it will ask user to enter again. Next if you enter some number it should come out of loop. It will keep you in infinite loop till you enter anything at command prompt.
 
I feel this loop is clean.
Avatar of 141ANG

ASKER

Is there a way to notify the user that they didn't enter anything? Rather than just offering them the prompt over and over?
SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 141ANG

ASKER

well that was simple enough, lol. It is amazing how many of these things can be so blaringly obvious one pointed out.
Avatar of 141ANG

ASKER

question again, and this may be obvious as well, why would I need to convert the strings to double twice? Or do I remove those completely from below, and only have them up above?
Avatar of 141ANG

ASKER

for instance should it look like this?
string TP = "";
	while (TP.size() == 0 )
	{
		cout << "Enter Current Gas Tax Percentage   ";
		getline(cin, TP);
	}
	cout << "You entered  " << TP << "%  ";
	double dTP = 0.0;
	std::stringstream ss1(TP); 
	ss1>>dTP;
	system("PAUSE");

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 141ANG

ASKER

thank you AOS, got it, now I just gotta figure out why the value always comes out the same.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 141ANG

ASKER

I just realized something by putting that string into the loop it moves it in one set of brackets which means that it is no longer at the same level or above the actual equeation, which I am assuming means that I need to adjust all of my brackets now.
>> I just realized something by putting that string into the loop it moves it in one set of brackets which means that it is no longer at the same level or above the actual equeation,

You don't need the string any more for your equation. You just need the double value, and you can define that before the loop.
Avatar of 141ANG

ASKER

1>c:\users\kristopher.roy\documents\code\projects\gastaxpromp\gastaxpromp\gasttaxprompt.cpp(18) : error C2061: syntax error : identifier 'fail'

1>c:\users\kristopher.roy\documents\code\projects\gastaxpromp\gastaxpromp\gasttaxprompt.cpp(25) : error C2039: 'fail' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>'
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 141ANG

ASKER

Here is a working copy however it is still returning a bad number. I have set the markers and am pasting the results below.
When it hits the double dTP = 0.0
Here are the results
+		TP	"8.5"	std::basic_string<char,std::char_traits<char>,std::allocator<char> >
 
		dTP	-9.2559631349317831e+061	double
 
 
Then when it hits ss1>>dTP
here are those results
 
+		TP	"8.5"	std::basic_string<char,std::char_traits<char>,std::allocator<char> >
 
		dTP	0.00000000000000000	double
 
+		ss1	{_Stringbuffer={...} }	std::basic_stringstream<char,std::char_traits<char>,std::allocator<char> >
 
Does that look normal, or is something wrong?
 
 
//============================================================================
// Name        : GASCHARGE.cpp
// Author      : Kristopher Roy
// Version     :
// Copyright   : 
// Description : Gas Charges
//============================================================================
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <istream>
#include <fstream>
#include <cstdlib>
using namespace std; 
vector<int> operands;
vector<string> actionOperator; 
int main(int argc, char *argv[])
{
string TP = "";
	while (TP.size() == 0 )
	{
		cout << "Enter Current Gas Tax Percentage   ";
		getline(cin, TP);
	}
	cout << "You entered  " << TP << "%  ";
	double dTP = 0.0;
	std::stringstream ss1(TP); 
	ss1>>dTP;
	system("PAUSE");
		{
			string GK;
			GK = "1";
			cout << "Please type 1 for Leaded or 2 for Unleaded ";
			string GC;
			getline(cin, GC);
			cout << "You entered " << GC << "  ";
			system("PAUSE");
			{if (GK == GC) 
				{
					cout << "Leaded Gas ";
					cout << "Please enter current Leaded Gas cost per Gallon ";
					string LGC;
					getline(cin, LGC);
					double dLGC = 0.0;
					std::stringstream ss2(LGC);
					ss2>>dLGC;
					cout << "You entered " <<"$" << LGC << " ";
					system ("PAUSE");
					{
						cout << "Please enter the gallons of gas pumped ";
						string LGP;
						getline (cin, LGP);		
						double dLGP = 0.0;
						std::stringstream ss3(LGP); 
						ss3>>dLGP;						
						cout << "You entered " << LGP << " gallons pumped ";
						system ("PAUSE");
						{			
							string PK;
							PK = "1";
							cout << "Enter 1 for Self Pumped or 2 for Employee Pumped? ";
							string PC;
							getline (cin, PC);
							cout << "You entered " << PC << "  ";
							system("Pause");
							if (PK == PC)
							{
								int P = 100;
								double result = ((((double)dTP)/P)*dLGC+dLGC)*dLGP;
								cout <<"The result is " "$" <<result<<endl;
							}
							else
							{
							string GNG;
							GNG = "1";
							cout << "Enter 1 for Gas-N-Go or 2 for Full-Service ";
							string ES;
							getline (cin, ES);
							cout << "You entered " << ES << "  ";
							system("Pause");
							if (GNG == ES)
								{				
									cout << "Gas-N-Go Service ";
									cout << "Please enter current Gas-N-Go Service Charge ";
									string GNGSC;
									// GNGCS stands for Gas-N-GO Service Charge
									getline(cin, GNGSC);
									cout << "You entered " <<"$" << GNGSC << " ";
									system ("PAUSE");
									{
										double dTP = 0.0;
										std::stringstream ss1(TP); 
										ss1>>dTP;
										double dLGC = 0.0;
										std::stringstream ss2(LGC);
										ss2>>dLGC;
										double dLGP = 0.0;
										std::stringstream ss3(LGP); 
										ss3>>dLGP;
										double dGNGSC = 0.0;
										std::stringstream ss4(GNGSC); 
										ss3>>dGNGSC;
										int P = 100;
										double result = ((((double)dTP)/P)*dLGC+dLGC)*dLGP+dGNGSC;
										cout <<"The result is " "$" <<result<<endl;
									}
								}
							}
						} 
					}  
				}
					else
				{
					cout << "Unleaded ";
					system ("PAUSE"); 
				}
			}
		}	
	cout << " Task is complete."; 
	system("PAUSE");
}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 141ANG

ASKER

seems to be working but I can't seem to get the CLS in the right place...

Finally put it after the whole section and it kind of works.
Avatar of 141ANG

ASKER

here is what I did with it,,,
	string TP = "";
	bool firstLoop = true;
    // break the line up wherever there are spaces
	while (TP.size() == 0 )
	{
		cout << "Enter Current Gas Tax Percentage   ";
		if (!firstLoop)
		cout << "you entered null " << endl;
		firstLoop = false;
		getline(cin, TP);
	}
		{
			system ("cls");

Open in new window

Your earliere question: "I want it to loop all the way back to the prompt rather than just allowing you to type "

system ("cls"); tries to address the above requirement.

This is what i'll do:-

      string TP= "";
      bool firstLoop = true;
      while(TP.size() == 0 )
      {
            system ("cls");
            if (!firstLoop)
            {
                  cout << "you entered null " << endl;
                  getch();
            }
            firstLoop = false;
            getline(cin, TP);
      }
     cout << "You entered  " << TP<< "%  ";
Avatar of 141ANG

ASKER

what does getch stand for?
It forces users to acknowldge the "you entered null " msg by hitting enter. It is a neat way of making sure that user reads it or had oppertunity to read/understand it.
Avatar of 141ANG

ASKER

cool what identifier do I need for getch to function?
Sorry! forgot to add that.
#include <conio.h>
Avatar of 141ANG

ASKER

is that a C code? is their a c++ version?
It should work irrespective of that. Do you get any error?

 
Avatar of 141ANG

ASKER

hey where do I put this line in at?
cout << "Enter Current Gas Tax Percentage   ";
You should really move the conversion to double INSIDE the loop. That way you can validate the user's input, and ask for new input if needed. Otherwise your code might still start processing invalid input, and fail in all kinds of strange ways.
Avatar of 141ANG

ASKER

I must be doing it wrong, I put them in the loop and they bombed.
>> I must be doing it wrong, I put them in the loop and they bombed.

Can you show what you tried ?
Avatar of 141ANG

ASKER

I will put it back in that way again, re-run it then get you the result. one min.
Just the code I meant.
Avatar of 141ANG

ASKER

Here you go.
1>(81) : error C2065: 'dTP' : undeclared identifier
1>(107) : error C2065: 'dTP' : undeclared identifier
1>(125) : error C2065: 'dTP' : undeclared identifier 
here is the code: 
	string TP= "";
	bool firstLoop = true;
    while(TP.size() == 0 )
	{
		double dTP = 0.0;
		std::stringstream ss1(TP); 
		ss1>>dTP;
		system ("cls");
		if (!firstLoop)
		{
			cout << "you entered null " << endl;
			getch();
		}
	firstLoop = false;
	getline(cin, TP);
	}
    cout << "You entered  " << TP<< "%  ";
	system("PAUSE");

Open in new window

hey where do I put this line in at?
cout << "Enter Current Gas Tax Percentage   ";

Answer:-      
                string TP= "";
      bool firstLoop = true;
      while(TP.size() == 0 )
      {
            system ("cls");
            cout << "Enter Current Gas Tax Percentage   ";
            if (!firstLoop)
            {
                  system ("cls");
                  cout << "You entered null. Please enter Current Gas Tax Percentage... " << endl;
                  getch();
            }
            firstLoop = false;
            getline(cin, TP);
      }
     cout << "You entered  " << TP<< "%  ";

      double dTP = 0.0;
      std::stringstream ss1(TP);
      ss1>>dTP;
Avatar of 141ANG

ASKER

was that what you needed Infinity?
Avatar of 141ANG

ASKER

thanks AOSS

You have declared double dTP = 0.0;
in the while loop.

It became local variable to that loop. Please keep the declaration part way above i.e. right after main() starts. and initialize the value 'dTP = 0.0;' wherever you want.
Avatar of 141ANG

ASKER

So what Infinity wants me to do won't work? Or am I not understanding what he wants me to do?
"You should really move the conversion to double INSIDE the loop" This is what Infinity  said. but not to declare it 'double dTP = 0.0;' inside the loop.

Do as following:

int main(int argc, char *argv[])
{
double dTP = 0.0;
// then rest of your code.
}
Avatar of 141ANG

ASKER

Like this?
int main(int argc, char *argv[])
 
{
	string TP= "";
	double dTP = 0.0;
	bool firstLoop = true;
    while(TP.size() == 0 )
	{
		std::stringstream ss1(TP); 
		ss1>>dTP;
		system ("cls");
		cout << "Enter Current Gas Tax Percentage   ";
		if (!firstLoop)
		{
			cout << "you entered null " << endl;
			getch();
		}
	firstLoop = false;
	getline(cin, TP);
	}

Open in new window

Avatar of 141ANG

ASKER

do it this way it doesn't error, but it also doesn't add in the tax. So there must be some sort of syntax issue.
Code below works. Actually control doesn't enter while loop when users enters something. So putting conversion inside loop will not work.
      
                string TP = "";
      bool firstLoop = true;
      double dTP = 0.0;
      while(TP.size() == 0 )
      {
            system ("cls");
            cout << "Enter Current Gas Tax Percentage   ";
            if (!firstLoop)
            {
                  system ("cls");
                  cout << "You entered null. Please enter Current Gas Tax Percentage... " << endl;
                  _getch();
            }
            firstLoop = false;
            getline(cin, TP);
      }
     cout << "You entered  " << TP << "%  ";


      std::stringstream ss1(TP);
      ss1>>dTP;

May be this is what infinity said:-

string TP = "";
bool firstLoop = true;
double dTP = 0.0;
while(TP.size() == 0 )
{
system ("cls");
cout << "Enter Current Gas Tax Percentage ";
if (!firstLoop)
{
system ("cls");
cout << "You entered null. Please enter Current Gas Tax Percentage... " << endl;
_getch();
}
firstLoop = false;
getline(cin, TP);
std::stringstream ss1(TP);
ss1>>dTP;
}
cout << "You entered " << TP << "% ";
May be this is what infinity said:-
      
string TP = "";
      bool firstLoop = true;
      double dTP = 0.0;
      while(TP.size() == 0 )
      {
            system ("cls");
            cout << "Enter Current Gas Tax Percentage   ";
            if (!firstLoop)
            {
                  system ("cls");
                  cout << "You entered null. Please enter Current Gas Tax Percentage... " << endl;
                  _getch();
            }
            firstLoop = false;
            getline(cin, TP);

            std::stringstream ss1(TP);
            ss1>>dTP;
      }
     cout << "You entered  " << TP << "%  ";
it works.
Avatar of 141ANG

ASKER

not sure but that first one you gave me is working. hopefully infinity can pop back in and clarify for us.
Avatar of 141ANG

ASKER

yes that worked as well.
so if I want this thing to clean up the screen inbetween each portion I just throw in a system ("cls")
maybe right after each of my pauses
I think I could get what infinity was talking about. The above/latest code pasted by me does give you the oppertunity of validating the user input.

Yes, system("cls") should be used as you understood.
You both seem to be making things more complicated than they need to be ;)
double value = 0.0;
std::cout << "Please enter the value : ";
while (1) {
  std::string line;
  getline(std::cin, line);
  std::stringstream ss(line);
  ss >> value;
  if (!(ss.fail())) break;
  std::cout << "Invalid input - please try again : ";
}
// at this point, 'value' contains a valid double value entered by the user

Open in new window

Avatar of 141ANG

ASKER

It would appear that there are many different ways to accomplish things, lol.
I am doing my best. So this set of code will accomplish the exact same thing as the code above?
>> It would appear that there are many different ways to accomplish things, lol.

There always are. But the first thing you have to think about, is how you want the code to behave. Then you think about the best way to implement that behavior.


>> So this set of code will accomplish the exact same thing as the code above?

I'm not sure what you mean by "the exact same thing" or by "the code above", but it's highly recommended to perform proper input validation at all times, and what I posted does that.
Avatar of 141ANG

ASKER

Infinity, did I get the syntax correct?
{
	string TP = "";
	bool firstLoop = true;
	double dTP = 0.0;
	while(TP.size() == 0 )
	cout << "Please Enter Current Gas Tax Percentage   ";
	while (1)
	{
		system ("cls");
		if (!firstLoop)
		{
			std::string line;
			getline(std::cin, line);
			std::stringstream TP(line);
			TP >> value;
			if (!(TP.fail())) break;
			std::cout << "Invalid input - please try again : ";
		}
			// at this point, 'value' contains a valid double value entered by the user
			std::stringstream ss1(TP);
			ss1>>dTP;
	}
	cout << "You entered  " << TP << "%  ";
	system("PAUSE");
	system("cls");

Open in new window

Avatar of 141ANG

ASKER

oops left the loop line in, how about this?
{
	string TP = "";
	bool firstLoop = true;
	double dTP = 0.0;
	while(TP.size() == 0 )
	cout << "Please Enter Current Gas Tax Percentage   ";
	while (1)
	{
		system ("cls");
		{
			std::string line;
			getline(std::cin, line);
			std::stringstream TP(line);
			TP >> value;
			if (!(TP.fail())) break;
			std::cout << "Invalid input - please try again : ";
		}
			// at this point, 'value' contains a valid double value entered by the user
			std::stringstream ss1(TP);
			ss1>>dTP;
	}
	cout << "You entered  " << TP << "%  ";
	system("PAUSE");
	system("cls");

Open in new window

Why are you still using the firstLoop variable ?
Why are you extracting data from a stringstream twice inside the loop ?

The code I posted wasn't just a copy-paste job. You need to understand it, and then adapt it to your code. Do you understand what the code I posted does ?

Try commenting each line of my code with your understanding of what happens, and then post that here.
Avatar of 141ANG

ASKER

shoot, still left the loop line in, lol.
Avatar of 141ANG

ASKER

before I work through this can you answer a very rudimentary question? Whenever a Dev puts ss it is merely a representation, in this case it is the same as the TP that I have been using. Is that correct? If not then I have my wires all crossed.
>> Whenever a Dev puts ss it is merely a representation

What do you mean by "representation" ? Do you mean a variable or instance ? If so, then the 'ss' in my code is indeed a variable - it is an instance of the std::stringstream type.
Avatar of 141ANG

ASKER

sorry, still learning terminology, every IT field has one, and I am in the military so that adds a whole other level of terminology all of its own, lol
Heh :) No worries.
Avatar of 141ANG

ASKER

ok looking through it I understand most of what it does, but the one that really stumps me is this
ss >> value;
the ss >> value means that ss is value? or value is also the same as ss?
however I am thinking value is not so much a command but rather a variable that I need to identify?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 141ANG

ASKER

The referenced documentation was could, but there is one thing that I didn't get
In that doc it merely uses string or stringstrem
but in your lines you use std::string and std::stringstream what is the difference?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 141ANG

ASKER

man you are a valuable resource of knowledge, thank you.
I try my best ;)
Avatar of 141ANG

ASKER

ok so based on the reading and what you are saying here is a revised code, my understanding of what is happening, and a few questions.
Line 1 defines the string as TP
Line 2 gets the typed info and tells the program that is what TP represents
Line 3 stringstream allows a string-based object to be treated as a stream making it easier to perform extraction or insertions
Line 4 tells the program that ss, which was TP is also value???
Line 5 - Not sure, except that I know this is the line that defines the failure in order to print out Line 6 on the screen.
If Line 5 succees what tells it to skip Line 6 and continue? If Line 5 fails what tells it to loop?

string TP;
		getline(cin, TP);
		stringstream ss(TP);
		ss >> value;
		if (!(ss.fail())) break;
		cout << "Invalid input - please try again : ";

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 141ANG

ASKER

OK, think I may have finaly got it
{
	double dTP = 0.0;
	string TP;
	cout << "Enter Current Gas Tax Percentage   ";
	while (1)
	{
		string value;	
		getline(cin, TP);
		stringstream ss1(TP);
		ss1 >> value;
		if (!(ss1.fail())) break;
		cout << "Invalid input - please try again : ";
	}
	// at this point, 'value' contains a valid double value entered by the user
	{
		stringstream ss1(TP);
		ss1>>dTP;
	}
	cout << "You entered  " << TP << "%  ";
	system("PAUSE");
	system("cls");
	{

Open in new window

>> OK, think I may have finaly got it

Not really ;)

The point of the loop is to ask input from the user repeatedly, until the user provides a valid double value. The loop will only end when the user has provided a valid double value. After the loop, dTP should contain that valid double value.

Take a closer look at what's happening now ... What are you trying to extract from the stringstream inside the loop ? Why is there a second stringstream extraction after the loop ? Part of the point was to avoid just that.

First try to understand my code (see my previous code) before you try to adapt it to fit in your code.
>> (see my previous code)

I meant "see my previous post"
Avatar of 141ANG

ASKER

I am going to take a look at it this morning but I have to fix our sharepoint server first, it appears to be down for some reason.
Avatar of 141ANG

ASKER

I see what you are talking about, for some reason I defined the same stringstream inside and outside of the loop.
Avatar of 141ANG

ASKER

I am slowly understanding, however I am still getting something wrong, I notice what I think is an issue, first, in order to maintain the value after the closing bracket, doesn't the rest of the code have to be in sub brackets of the code?
{
	double dTP = 0.0;
	cout << "Enter Current Gas Tax Percentage   ";
	while (1)
	{
		string TP;	
		getline(cin, TP);
		stringstream ss1(TP);
		ss1 >> dTP;
		if (!(ss1.fail())) break;
		cout << "Invalid input - please try again : ";
	}
	// at this point, 'value' contains a valid double value entered by the user
	cout << "You entered  " << TP << "%  ";
	system("PAUSE");
	system("cls");

Open in new window

Avatar of 141ANG

ASKER

like this?
{
	double dTP = 0.0;
	cout << "Enter Current Gas Tax Percentage   ";
	while (1)
	{
		string TP;	
		getline(cin, TP);
		stringstream ss1(TP);
		ss1 >> dTP;
		if (!(ss1.fail())) break;
		cout << "Invalid input - please try again : ";
		{
			// at this point, 'value' contains a valid double value entered by the user
			cout << "You entered  " << TP << "%  ";
			system("PAUSE");
			system("cls");
		}
	}

Open in new window

Avatar of 141ANG

ASKER

never mind that doesn't work, something just isn't clicking for me.
Avatar of 141ANG

ASKER

ok I think I finaly figured it out. as long as I set string x at the same level or above the level that I make reference to x it will function, and the values that are input must be at the same level or above the level that they are referenced, or used.
{
	string TP;	
	double dTP = 0.0;
	cout << "Enter Current Gas Tax Percentage   ";
	while (1)
	{
		getline(cin, TP);
		stringstream ss1(TP);
		ss1 >> dTP;
		if (!(ss1.fail())) break;
		cout << "Invalid input - please try again : ";
	}
	{
		// at this point, 'value' contains a valid double value entered by the user
		cout << "You entered  " << TP << "%  ";
		system("PAUSE");
		system("cls");
	}

Open in new window

Avatar of 141ANG

ASKER

Question?
If I wanted cin to only allow 1 or 2 as possible options and fail if neither of those is selected how would I code that? I have been reading through all of the documentation on that CPlusPlus site which says that this is possible, but I can't find the answer to how.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 141ANG

ASKER

I have completely cleaned up the code as per your suggestion, below is the code. I am now going to try and figure out how to do the int validation.
//============================================================================
// Name        : GASCHARGE.cpp
// Author      : Kristopher Roy
// Version     :
// Copyright   : 
// Description : Gas Charges
//============================================================================
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <istream>
#include <fstream>
#include <cstdlib>
#include <conio.h>
 
using namespace std; 
vector<int> operands;
vector<string> actionOperator; 
 
int main(int argc, char *argv[])
 
{
	double dTP = 0.0;
	cout << "Enter Current Gas Tax Percentage   ";
	while (1)
	{
		string TP;
		//TP stands for Tax Percentage
		getline(cin, TP);
		stringstream ss1(TP);
		ss1 >> dTP;
		if (!(ss1.fail())) break;
		cout << "Invalid input - please try again : ";
	}
	{
		// at this point, 'dTP' contains a valid double value entered by the user
		cout << "You entered  " << dTP << "%  ";
		system("PAUSE");
		system("cls");
	}
	{
		//GK stands for Gas Kind
		string GK;
		GK = "1";
		cout << "Please type 1 for Leaded or 2 for Unleaded ";
		//GC stands for Gas Choice
		string GC;
		getline(cin, GC);
		cout << "You entered " << GC << "  ";
		system("PAUSE");
		{
			if (GK == GC)
			{
				cout << "Leaded Gas ";
				double dLGC = 0.0;
				cout << "Please enter current Leaded Gas cost per Gallon ";
				while (1)
				{
					//LGC stands for Leaded Gas Cost				
					string LGC;				
					getline(cin, LGC);
					stringstream ss2(LGC);
					ss2>>dLGC;
					if (!(ss2.fail())) break;
					cout << "Invalid input - please try again : ";
				}
				{
					// at this point, 'dLGC' contains a valid double value entered by the user
					cout << "You entered " <<"$" << dLGC << " ";
					system ("PAUSE");
					system ("cls");
				}
				{
					cout << "Please enter the gallons of gas pumped ";
					double dLGP = 0.0;
					while (1)
					{
						// LGP stands for Leaded Gas Pumped
						string LGP;
						getline (cin, LGP);		
						stringstream ss3(LGP); 
						ss3>>dLGP;
						if (!(ss3.fail())) break;
						cout << "Invalid input - please try again : ";
					}
					{
						// at this point, 'dLGP' contains a valid double value entered by the user
						cout << "You entered " << dLGP << " gallons pumped ";
						system ("PAUSE");
						system ("cls");
					}
					{
						//PK stands for Pumping Kind
						string PK;
						PK = "1";
						cout << "Enter 1 for Self Pumped or 2 for Employee Pumped? ";
						//PC stands for Pumping Choice
						string PC;
						getline (cin, PC);
						cout << "You entered " << PC << "  ";
						system("Pause");
						if (PK == PC)
						{
							int P = 100;
							double result = ((((double)dTP)/P)*dLGC+dLGC)*dLGP;
							cout <<"The Cost of Self Pumped Fueling today is " "$" <<result<<endl;
							// Result of Self Service Leaded Gas
						}
						else
						{
							//
							string GNG;
							GNG = "1";
							cout << "Enter 1 for Gas-N-Go or 2 for Full-Service ";
							string ES;
							getline (cin, ES);
							cout << "You entered " << ES << "  ";
							system("Pause");
							if (GNG == ES)
							{				
								cout << "Gas-N-Go Service ";
								cout << "Please enter current Gas-N-Go Service Charge ";
								double dGNGSC = 0.0;
								while (1)
								{
									// GNGSC stands for Gas-N-GO Service Charge
									string GNGSC;								
									getline(cin, GNGSC);
									stringstream ss4(GNGSC); 
									ss4>>dGNGSC;
									if (!(ss4.fail())) break;
									cout << "Invalid input - please try again : ";
								}
								{
									cout << "You entered " <<"$" << dGNGSC << " ";
									system ("PAUSE");
									system ("cls");
									{
										int P = 100;
										double result = ((((double)dTP)/P)*dLGC+dLGC)*dLGP+dGNGSC;
										cout <<"The Cost of Gas-N-Go Fueling today is  " "$" <<result<<endl;
										// Result of Gas-N-Go Leaded Gas
									}
								}
							}	
							else
							{				
								cout << "Full-Service ";
								cout << "Please enter current Full-Service Charge ";
								getline(cin, FSSC);
								double dFSSC = 0.0;
								while (1)
								{
									string FSSC;
									// FSSC stands for Full-Service Service Charge
									stringstream ss5(FSSC); 
									ss5>>dFSSC;
									if (!(ss5.fail())) break;
									cout << "Invalid input - please try again : ";
								}
								{
									// at this point, 'value' contains a valid double value entered by the user
									cout << "You entered " <<"$" << dFSSC << " ";
									system ("PAUSE");
									system ("cls");
								}
								{
									int P = 100;
									double result = ((((double)dTP)/P)*dLGC+dLGC)*dLGP+dFSSC;
									cout <<"The Cost of Full-Service Fueling today is " "$" <<result<<endl;
									// Result of Full-Service Leaded Gas
								}
							}
						}
					} 
				}  
			}
			else
			{
				cout << "Unleaded ";
				system ("PAUSE"); 
			}
		}
	}	
	cout << " Task is complete."; 
	system("PAUSE");
}

Open in new window

>> I have completely cleaned up the code as per your suggestion, below is the code

Nice. That looks a lot cleaner, doesn't it ? ;)


>> I am now going to try and figure out how to do the int validation.

You can get your inspiration from the double input validation, except that you'll need one extra step to check the actual value of the int too.
Avatar of 141ANG

ASKER

like this?
int a, b;
Avatar of 141ANG

ASKER

     how about this, would this work?
	{
		//GK stands for Gas Kind
		string GK;
		GK = "1";
		cout << "Please type 1 for Leaded or 2 for Unleaded ";
		//GC stands for Gas Choice
		string GC;
		dowhile getline(cin, GC);
		cout << "You entered " << GC << "  ";
		system("PAUSE");
		{
			if (GK == GC)
			{

Open in new window

Avatar of 141ANG

ASKER

ok, I obviously had that all wrong, here is my next attempt, while something is still wrong, I think I am getting closer.
{
		//GK stands for Gas Kind
		string GK;
		GK = "1";
		string GC;
		cout << "Please type 1 for Leaded or 2 for Unleaded ";
		do
		{
			//GC stands for Gas Choice
			getline(cin, GC);
			cout << "You entered " << GC << "  ";
		}
		while((GC !='1')&&(GC !='2'));
		system("PAUSE");
		
		{
			if (GK == GC)

Open in new window

Avatar of 141ANG

ASKER

ok I have it now, take a look... Now I just need to go through the rest of the prompts and do the same.
//============================================================================
// Name        : GASCHARGE.cpp
// Author      : Kristopher Roy
// Version     :
// Copyright   : 
// Description : Gas Charges
//============================================================================
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <istream>
#include <fstream>
#include <cstdlib>
#include <conio.h> 
using namespace std; 
vector<int> operands;
vector<string> actionOperator;  
int main(int argc, char *argv[]) 
{
	double dTP = 0.0;
	cout << "Enter Current Gas Tax Percentage   ";
	while (1)
	{
		string TP;
		//TP stands for Tax Percentage
		getline(cin, TP);
		stringstream ss1(TP);
		ss1 >> dTP;
		if (!(ss1.fail())) break;
		cout << "Invalid input - please try again : ";
	}
	{
		// at this point, 'dTP' contains a valid double value entered by the user
		cout << "You entered  " << dTP << "%  ";
		system("PAUSE");
		system("cls");
	}
	{
		//GK stands for Gas Kind
		string GK;
		GK = "1";
		string GC;
		cout << "Please type 1 for Leaded or 2 for Unleaded ";
		do
		{
			//GC stands for Gas Choice
			getline(cin, GC);
			cout << "You entered " << GC << "  ";
		}
		while((GC !="1")&&(GC !="2"));
		system("PAUSE");
		
		{
			if (GK == GC)
			{
				cout << "Leaded Gas ";
				double dLGC = 0.0;
				cout << "Please enter current Leaded Gas cost per Gallon ";
				while (1)
				{
					//LGC stands for Leaded Gas Cost				
					string LGC;				
					getline(cin, LGC);
					stringstream ss2(LGC);
					ss2>>dLGC;
					if (!(ss2.fail())) break;
					cout << "Invalid input - please try again : ";
				}
				{
					// at this point, 'dLGC' contains a valid double value entered by the user
					cout << "You entered " <<"$" << dLGC << " ";
					system ("PAUSE");
					system ("cls");
				}
				{
					cout << "Please enter the gallons of gas pumped ";
					double dLGP = 0.0;
					while (1)
					{
						// LGP stands for Leaded Gas Pumped
						string LGP;
						getline (cin, LGP);		
						stringstream ss3(LGP); 
						ss3>>dLGP;
						if (!(ss3.fail())) break;
						cout << "Invalid input - please try again : ";
					}
					{
						// at this point, 'dLGP' contains a valid double value entered by the user
						cout << "You entered " << dLGP << " gallons pumped ";
						system ("PAUSE");
						system ("cls");
					}
					{
						//PK stands for Pumping Kind
						string PK;
						PK = "1";
						cout << "Enter 1 for Self Pumped or 2 for Employee Pumped? ";
						//PC stands for Pumping Choice
						string PC;
						getline (cin, PC);
						cout << "You entered " << PC << "  ";
						system("Pause");
						if (PK == PC)
						{
							int P = 100;
							double result = ((((double)dTP)/P)*dLGC+dLGC)*dLGP;
							cout <<"The Cost of Self Pumped Fueling today is " "$" <<result<<endl;
							// Result of Self Service Leaded Gas
						}
						else
						{
							//
							string GNG;
							GNG = "1";
							cout << "Enter 1 for Gas-N-Go or 2 for Full-Service ";
							string ES;
							getline (cin, ES);
							cout << "You entered " << ES << "  ";
							system("Pause");
							if (GNG == ES)
							{				
								cout << "Gas-N-Go Service ";
								cout << "Please enter current Gas-N-Go Service Charge ";
								double dGNGSC = 0.0;
								while (1)
								{
									// GNGSC stands for Gas-N-GO Service Charge
									string GNGSC;								
									getline(cin, GNGSC);
									stringstream ss4(GNGSC); 
									ss4>>dGNGSC;
									if (!(ss4.fail())) break;
									cout << "Invalid input - please try again : ";
								}
								{
									cout << "You entered " <<"$" << dGNGSC << " ";
									system ("PAUSE");
									system ("cls");
									{
										int P = 100;
										double result = ((((double)dTP)/P)*dLGC+dLGC)*dLGP+dGNGSC;
										cout <<"The Cost of Gas-N-Go Fueling today is  " "$" <<result<<endl;
										// Result of Gas-N-Go Leaded Gas
									}
								}
							}	
							else
							{				
								cout << "Full-Service ";
								cout << "Please enter current Full-Service Charge ";
								double dFSSC = 0.0;
								while (1)
								{
									string FSSC;
									// FSSC stands for Full-Service Service Charge
									stringstream ss5(FSSC); 
									ss5>>dFSSC;
									if (!(ss5.fail())) break;
									cout << "Invalid input - please try again : ";
								}
								{
									// at this point, 'value' contains a valid double value entered by the user
									cout << "You entered " <<"$" << dFSSC << " ";
									system ("PAUSE");
									system ("cls");
								}
								{
									int P = 100;
									double result = ((((double)dTP)/P)*dLGC+dLGC)*dLGP+dFSSC;
									cout <<"The Cost of Full-Service Fueling today is " "$" <<result<<endl;
									// Result of Full-Service Leaded Gas
								}
							}
						}
					} 
				}  
			}
			else
			{
				cout << "Unleaded ";
				system ("PAUSE"); 
			}
		}
	}	
	cout << " Task is complete."; 
	system("PAUSE");
}

Open in new window

>> ok I have it now, take a look...

Right. You're not actually doing integer validation, but it should work.
Avatar of 141ANG

ASKER

integer validation uses less memory correct?
Avatar of 141ANG

ASKER

Here is the whole thing start to finish, any suggestions for improvement?
//============================================================================
// Name        : GASCHARGE.cpp
// Author      : Kristopher Roy
// Version     :
// Copyright   : 
// Description : Gas Charges
//============================================================================
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <istream>
#include <fstream>
#include <cstdlib>
#include <conio.h> 
using namespace std; 
vector<int> operands;
vector<string> actionOperator;  
int main(int argc, char *argv[]) 
{
	double dTP = 0.0;
	cout << "Enter Current Gas Tax Percentage   ";
	while (1)
	{
		string TP;
		//TP stands for Tax Percentage
		getline(cin, TP);
		stringstream ss1(TP);
		ss1 >> dTP;
		if (!(ss1.fail())) break;
		cout << "Invalid input - please try again : ";
	}
	{
		// at this point, 'dTP' contains a valid double value entered by the user
		cout << "You entered  " << dTP << "%  ";
		system("PAUSE");
		system("cls");
	}
	{
		//GK stands for Gas Kind
		string GK;
		GK = "1";
		string GC;
		do
		{
			cout << "Please type 1 for Leaded or 2 for Unleaded ";		
			//GC stands for Gas Choice
			getline(cin, GC);
			cout << "You entered " << GC << "  ";
			system("Pause");
			system("CLS");
		}
		while((GC !="1")&&(GC !="2"));
		system("PAUSE");
		system("cls");
		
		{
			if (GK == GC)
			{
				cout << "Leaded Gas ";
				system("pause");
				double dLGC = 0.0;
				cout << "Please enter current Leaded Gas cost per Gallon ";
				while (1)
				{
					//LGC stands for Leaded Gas Cost				
					string LGC;				
					getline(cin, LGC);
					stringstream ss2(LGC);
					ss2>>dLGC;
					if (!(ss2.fail())) break;
				}
				{
					// at this point, 'dLGC' contains a valid double value entered by the user
					cout << "You entered " <<"$" << dLGC << " ";
					system ("PAUSE");
					system ("cls");
				}
				{
					cout << "Please enter the gallons of gas pumped ";
					double dLGP = 0.0;
					while (1)
					{
						// LGP stands for Leaded Gas Pumped
						string LGP;
						getline (cin, LGP);		
						stringstream ss3(LGP); 
						ss3>>dLGP;
						if (!(ss3.fail())) break;
						cout << "Invalid input - please try again : ";
					}
					{
						// at this point, 'dLGP' contains a valid double value entered by the user
						cout << "You entered " << dLGP << " gallons pumped ";
						system ("PAUSE");
						system ("cls");
					}
					{
						//PK stands for Pumping Kind
						string PK;
						PK = "1";
						string PC;
						//PC stands for Pumping Choice
						do
						{
							cout << "Enter 1 for Self Pumped or 2 for Employee Pumped? ";						
							getline (cin, PC);
							cout << "You entered " << PC << "  ";
							system("Pause");
							system("cls");	
						}
						while ((PC !="1")&&(PC !="2"));
						system("Pause");
						if (PK == PC)
						{
							int P = 100;
							double result = ((((double)dTP)/P)*dLGC+dLGC)*dLGP;
							cout <<"The Cost of Self Pumped Fueling today is " "$" <<result<<endl;
							// Result of Self Service Leaded Gas
						}
						else
						{
							//GNG stands for Gas-N-Go
							string GNG;
							GNG = "1";
							//ES stands for Employee Service	
							string ES;
							do
							{
								cout << "Enter 1 for Gas-N-Go or 2 for Full-Service ";
								getline (cin, ES);
								cout << "You entered " << ES << "  ";
								system("Pause");
								system("CLS");
							}
							while ((ES !="1")&&(ES !="2"));
							system("Pause");
							if (GNG == ES)
							{				
								cout << "Gas-N-Go Service ";
								cout << "Please enter current Gas-N-Go Service Charge ";
								double dGNGSC = 0.0;
								while (1)
								{
									// GNGSC stands for Gas-N-GO Service Charge
									string GNGSC;								
									getline(cin, GNGSC);
									stringstream ss4(GNGSC); 
									ss4>>dGNGSC;
									if (!(ss4.fail())) break;
									cout << "Invalid input - please try again : ";
								}
								{
									cout << "You entered " <<"$" << dGNGSC << " ";
									system ("PAUSE");
									system ("cls");
									{
										int P = 100;
										double result = ((((double)dTP)/P)*dLGC+dLGC)*dLGP+dGNGSC;
										cout <<"The Cost of Gas-N-Go Fueling today is  " "$" <<result<<endl;
										// Result of Gas-N-Go Leaded Gas
									}
								}
							}	
							else
							{				
								cout << "Full-Service ";
								cout << "Please enter current Full-Service Charge ";
								double dFSSC = 0.0;
								while (1)
								{
									string FSSC;
									// FSSC stands for Full-Service Service Charge
									stringstream ss5(FSSC); 
									ss5>>dFSSC;
									if (!(ss5.fail())) break;
									cout << "Invalid input - please try again : ";
								}
								{
									// at this point, 'value' contains a valid double value entered by the user
									cout << "You entered " <<"$" << dFSSC << " ";
									system ("PAUSE");
									system ("cls");
								}
								{
									int P = 100;
									double result = ((((double)dTP)/P)*dLGC+dLGC)*dLGP+dFSSC;
									cout <<"The Cost of Full-Service Fueling today is " "$" <<result<<endl;
									// Result of Full-Service Leaded Gas
								}
							}
						}
					} 
				}  
			}
			else
			{
				cout << "Unleaded ";
				system ("PAUSE"); 
				double dULGC = 0.0;
				cout << "Please enter current UnLeaded Gas cost per Gallon ";
				while (1)
				{
					//ULGC stands for UnLeaded Gas Cost				
					string ULGC;				
					getline(cin, ULGC);
					stringstream ss2(ULGC);
					ss2>>dULGC;
					if (!(ss2.fail())) break;
				}
				{
					// at this point, 'dULGC' contains a valid double value entered by the user
					cout << "You entered " <<"$" << dULGC << " ";
					system ("PAUSE");
					system ("cls");
				}
				{
					cout << "Please enter the gallons of gas pumped ";
					double dULGP = 0.0;
					while (1)
					{
						// ULGP stands for UnLeaded Gas Pumped
						string ULGP;
						getline (cin, ULGP);		
						stringstream ss3(ULGP); 
						ss3>>dULGP;
						if (!(ss3.fail())) break;
						cout << "Invalid input - please try again : ";
					}
					{
						// at this point, 'dULGP' contains a valid double value entered by the user
						cout << "You entered " << dULGP << " gallons pumped ";
						system ("PAUSE");
						system ("cls");
					}
					{
						//PK stands for Pumping Kind
						string PK;
						PK = "1";
						string PC;
						//PC stands for Pumping Choice
						do
						{
							cout << "Enter 1 for Self Pumped or 2 for Employee Pumped? ";						
							getline (cin, PC);
							cout << "You entered " << PC << "  ";
							system("Pause");
							system("cls");	
						}
						while ((PC !="1")&&(PC !="2"));
						system("Pause");
						if (PK == PC)
						{
							int P = 100;
							double result = ((((double)dTP)/P)*dULGC+dULGC)*dULGP;
							cout <<"The Cost of Self Pumped Fueling today is " "$" <<result<<endl;
							// Result of Self Service UnLeaded Gas
						}
						else
						{
							//GNG stands for Gas-N-Go
							string GNG;
							GNG = "1";
							//ES stands for Employee Service	
							string ES;
							do
							{
								cout << "Enter 1 for Gas-N-Go or 2 for Full-Service ";
								getline (cin, ES);
								cout << "You entered " << ES << "  ";
								system("Pause");
								system("CLS");
							}
							while ((ES !="1")&&(ES !="2"));
							system("Pause");
							if (GNG == ES)
							{				
								cout << "Gas-N-Go Service ";
								cout << "Please enter current Gas-N-Go Service Charge ";
								double dGNGSC = 0.0;
								while (1)
								{
									// GNGSC stands for Gas-N-GO Service Charge
									string GNGSC;								
									getline(cin, GNGSC);
									stringstream ss4(GNGSC); 
									ss4>>dGNGSC;
									if (!(ss4.fail())) break;
									cout << "Invalid input - please try again : ";
								}
								{
									cout << "You entered " <<"$" << dGNGSC << " ";
									system ("PAUSE");
									system ("cls");
									{
										int P = 100;
										double result = ((((double)dTP)/P)*dULGC+dULGC)*dULGP+dGNGSC;
										cout <<"The Cost of Gas-N-Go Fueling today is  " "$" <<result<<endl;
										// Result of Gas-N-Go UnLeaded Gas
									}
								}
							}	
							else
							{				
								cout << "Full-Service ";
								cout << "Please enter current Full-Service Charge ";
								double dFSSC = 0.0;
								while (1)
								{
									string FSSC;
									// FSSC stands for Full-Service Service Charge
									stringstream ss5(FSSC); 
									ss5>>dFSSC;
									if (!(ss5.fail())) break;
									cout << "Invalid input - please try again : ";
								}
								{
									// at this point, 'value' contains a valid double value entered by the user
									cout << "You entered " <<"$" << dFSSC << " ";
									system ("PAUSE");
									system ("cls");
								}
								{
									int P = 100;
									double result = ((((double)dTP)/P)*dULGC+dULGC)*dULGP+dFSSC;
									cout <<"The Cost of Full-Service Fueling today is " "$" <<result<<endl;
									// Result of Full-Service Leaded Gas
								}
							}
						}
					} 
				}  
			}
		}
	}	
	cout << " Task is complete."; 
	system("PAUSE");
}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 141ANG

ASKER

Infinity, these are great suggestions, and exactly what I would like to do. I am going to close this thread, but I appriciate all of your help.
Avatar of 141ANG

ASKER

What a great wealth of knowledge thanks to all who helped...
Glad to be of assistance.