Create a user input error message

State that the user input an invalid value

Page 1 of 1

6 Replies - 2545 Views - Last Post: 06 April 2008 - 06:04 PM Rate Topic: -----

#1 Debbie44  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-April 08

Create a user input error message

Posted 05 April 2008 - 07:19 PM

I am learning very basic C from a manual. The code compiles, builds and executes, but doesn't do exactly what I want it to do. I would like it to display a message to the user if they select an value outside of my specified range, but right now the message comes up even for valid input values. Please help.

#include <stdio.h>
main()
{
float USDollar;
float Euro;
USDollar = 1.5787;
Euro = 1.0000;
printf("\nEnter Number of Euros between 1 and 1000: ");
scanf("%f", &Euro);
if ( &Euro < 1 || &Euro > 1000 ){
printf("\nThe number you selected is not in the specified range\n");
}
else {
printf("The equivalency in US Dollars is %f\n", USDollar * Euro);
}
fflush(stdin);
printf("\nPress Enter key to end.");
getchar();
}


*edit: Please use code tags in the future, thanks! :code:

This post has been edited by Martyr2: 05 April 2008 - 07:30 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Create a user input error message

#2 Debbie44  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-April 08

Re: Create a user input error message

Posted 05 April 2008 - 07:41 PM

Thank you Marty. I read that in the instructions and forgot to do it. I promise I will remember those tags in any future posts. Hopefully someone can help me with this one though.
Was This Post Helpful? 0
  • +
  • -

#3 Cerolobo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 450
  • Joined: 05-April 08

Re: Create a user input error message

Posted 05 April 2008 - 07:42 PM

Your issue is on line 10.

&Euro retrieves the address of the variable (where it is in the computer memory), which is always going to be greater then 1000.

The reason why you use &Eruo in scanf(), is so scanf() can modify your variable to contain what the user typed in.

Pointers can be fairly hard to understand when you start out, so you might want to do so addition reading on it.

For example
http://www.physics.d...asics/#pointers
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,140
  • Joined: 14-September 07

Re: Create a user input error message

Posted 05 April 2008 - 07:53 PM

Don't use &Euro on line 10 this should make it work:

 if ( (Euro < 1) || (Euro > 1000) ){  
       printf("\nThe number you selected is not in the specified range\n");  
 } 


Was This Post Helpful? 0
  • +
  • -

#5 bad_karma00  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 10-March 08

Re: Create a user input error message

Posted 06 April 2008 - 05:50 PM

Actually, I think the following:
if( Euro < 1 || Euro > 1000 )
should be:
if (Euro < 1 && Euro > 1000 )

#include <stdio.h>  
main()  
{  
float USDollar;  
float Euro;  
USDollar = 1.5787;  
Euro = 1.0000;  
printf("\nEnter Number of Euros between 1 and 1000: ");  
scanf("%f", &Euro);  
if ( Euro < 1 && Euro > 1000 ){  
printf("\nThe number you selected is not in the specified range\n");  
}  
else {  
printf("The equivalency in US Dollars is %f\n", USDollar * Euro);  
}  
fflush(stdin);  
printf("\nPress Enter key to end.");  
getchar();  
}


This post has been edited by bad_karma00: 06 April 2008 - 05:50 PM

Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Create a user input error message

Posted 06 April 2008 - 05:53 PM

I think if( Euro < 1 || Euro > 1000 ) is the correct format, because something cant be less than 1 and greater than 1000 at the same time

This post has been edited by PsychoCoder: 06 April 2008 - 05:53 PM

Was This Post Helpful? 0
  • +
  • -

#7 bad_karma00  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 10-March 08

Re: Create a user input error message

Posted 06 April 2008 - 06:04 PM

I apolgize, you're correct.

I cleaned up your code a bit and added a loop so the user is able to calculate more than one value in a session. If it doesn't work let me know. I may have nested the if else statements incorrectly; if so just let me know.

#include<iomanip>
#include<iostream>
#include <stdio.h>  

using namespace std;

int main()
{
	int stop;
	char end, q, Q;
	float USDollar;
	float Euro;
	USDollar = 1.5787;
	Euro = 1.0000;
	
	while(true)
{
{
	cout << "Press Q to exit or any number to continue" << endl;
	cin >> end;	
}   
	if( end == 'Q' || end == 'q' )
{
	return 0;
}
	else
{
	cout << "\n\nEnter Number of Euros between 1 and 1000: " << endl;
	cin >> Euro;
}
	
	if( Euro < 1 || Euro > 1000 )
{
	cout <<"\nThe number you selected is not in the specified range\n" << endl;
}
	else
{
	cout << "\n\nThe equivalency in US Dollars is:\n" << USDollar * Euro << endl;
	cout << "------------------------------------" << endl;
}
}   

	cin >> stop;
	return 0;
}


This post has been edited by bad_karma00: 06 April 2008 - 07:06 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1