Hello i'm very new to programming and self teaching myself. I'm having a problem with my project. I'm trying to preform an equation, but every time I
in put the variable it runs to the end. It is a programm to help solve precents
for my friend. This is the code:

// Percent
// To find percents by an equation

#include <iostream>


using namespace std;

int main()
{
    int percent;
    int amount;
    int base;
    int answer;

    cout << "\nThis is a program is to find percents using cross multiplication";
    cout << "\nIn this program you will only use the variable 'n'";
    cout << "\nThis will use a Precent, Base and Amount";
    cout << "\nThe set up of this is like so:\n";
    cout << "\n\tP   A";
    cout << "\n\t-   -";
    cout << "\n       100  B\n";
    cout << "\nWhen using this program only use one variable, because if you";
    cout << "use more than one, you will screw it up.";
    cout << "\nIf you say p = 'n' and a = 'n' you can't get any product. ";
    cout << "It's like Bob Caldwelltrying to tell the weather...it just don't work";


    cout << "\n\nEnter the Percent: ";
    cin >> percent;


    cout << "\nEnter the Amount: ";
    cin >> amount;

    cout << "\nEnter the Base: ";
    cin >> base;


    if ( percent == 'n' )        //the equation if percent is 'n'
    {   
         answer = 100 * amount /base;
         cout << "\n\nThe answer is: " << answer << " %" << endl;
    }



    if ( amount =='n' )          // the equation if amount is 'n'
    {         
         answer =(percent * base) / 100;
         cout << "\n\nThe Amount is: " << answer << endl;
    }



    if ( base == 'n' )           //the equation if the base is 'n'
    {
         answer = (100 * amount) / percent;              
         cout << "\n\nThe base is: " << answer << endl;
    }



    cout << "\n\nAnswer: " << answer;          //restate the answer
    cout << "\n\nThank you" << endl;

    return 0;
}

I don't know what to do? can anyone help me. If I enter 'n' as the percent it
runs to the end. It also runs to the end when I enter it in any other field. So please help me. :confused:

Recommended Answers

All 6 Replies

Although

if (percent == 'n')

is not incorrect, it is unusual though to compare a char against an int. In this case the only way this would be true if you entered 110 as lower case n has that value.

Change your conditionals so you are comparing against numeric values

if (percent == 0)

Also with VC++

cin >> percent;

and then entering a character sends my system into a tailspin after entering that at the precentage prompt.

>Also with VC++
That happens everywhere. cin's >> operator determines how to accept input by the type of its arguments. If you say you'll enter a number but you actually enter a character (which is an invalid number), it only makes sense that the call should fail.

That happens everywhere.

I thought so, but wanted to qualify as I've have no means by which to verify using Borland, g++ or anything else.

it only makes sense that the call should fail.

I was surprised though that based on the aforementioned code that once I entered 'n' at the percentage prompt that "cin" fell through the other two prompts.

(1) Do you know why this is?

(2) How would one code appropriately to circumvent. I assume by flushing the input buffer before next call to cin.

Here is an example that addresses theory of opertion. NOTE: not done in pure C++, especially as it pertains to cout, so don't use for an assignment.

I choose to use float instead of int, only in so much as it provides a greater level of precision.

#include <iostream>
using namespace std;
 
char *Prompt = "\n\n\t\t[P] Percent\n\t\t[A] Amount\n\t\t[B] Base\n\t\t[Q] Quit\n";
 
int main()
{
char Selection;
float Ratio, Amount, Base;
 
cout << Prompt;
 
do
{
cout << "\n\n\t\tSelect [ ]\b\b";
cin >> Selection;
cout << endl << "-------------------------------" << endl;
 
switch (Selection & 0x5f)
{
case 'P':
	cout << "\t\tAmount: ";
	cin >> Amount;
	cout << "\t\t Base: ";
	cin >> Base;
	cout << "\t\t\t" << (Amount / Base) * 100 << "%" << endl;
	break;
 
case 'A':
	cout << "\t\tPercentage: ";
	cin >> Ratio;
 
	if (Ratio > 1)
	 Ratio /= 100;
 
	cout << "\t\t	 Base: ";
	cin >> Base;
	cout << Base * Ratio << endl;
	break;
 
case 'B':
	cout << "\t\tPercentage: ";
	cin >> Ratio;
 
	if (Ratio > 1)
	 Ratio /= 100;
 
	 cout << "\t\t	Amount:";
	 cin >> Amount;
	 cout << "\t\t" << Amount / Ratio << endl;
	 break;
 
case 'Q':
	Selection = 0;
	break;
 
default:
	cout << "Invalid Funciton, Please select again" << endl;
	break;
}
 
} while (Selection != 0);
 
cout << "\n\n\nThank-you, Good-bye" << endl;
return 0;
}

A good exercise would be to clean up output using C++ qualifiers and eliminate redundancy "if (Ratio > 1)".

:cheesy: Man you are brilliant!!!! I never thought of using a switch statemnet.
Thank you so very much!!!!!!!!

>(1) Do you know why this is?
The stream is in a failure state, any further use will also fail.

>(2) How would one code appropriately to circumvent.
Traditionally, we clear the stream of errors (by unsetting all failure bits) and discard any bad characters (usually by "flushing" the stream):

if (!(cin>> x)) {
  cerr<<"Invalid input"<<endl;
  cin.clear();
  cin.ignore(cin.rdbuf()->in_avail());
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.