473,398 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

& non lvalue error

88
I'm just learning C, and I'm trying to get this simple program to validate the user's entry making sure that they entered a number.

I compile and I get this:

Miracle C Compiler (r3.2), written by bts.
Compiling C:\Users\Franccesca\Documents\Code\userInput.c
main

C:\Users\Franccesca\Documents\Code\userInput.c
line 13: & non lvalue
'scanf("%s", &buff)'
aborting compile

Here is my code, and I can't figure out why it isn't working. Does anyone have any ideas?

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3.  
  4. main () {
  5. char buff[10];
  6. int validInput; //1 = Good , 2 = Bad
  7. int i;
  8.  
  9.  
  10. //GET PURCHASE AMOUNT FROM USER
  11. do {
  12.     printf("\nPlease enter purchase amount:", stdout);
  13.     scanf("%s", &buff);
  14.     validInput = 1; //SET TO VALID TO START
  15.  
  16.     // IF NOTHING ENTERED
  17.         if(lstrlen(buff) == 0) validInput = 2;
  18.         for(i = 0; i < lstrlen(buff); i++){
  19.                 if(buff[i] != 46){ // IF NOT A DECIMAL POINT
  20.                    if((buff[i] < 48) || (buff[i] > 57)){   //IF NOT 0 - 9
  21.                       validInput = 2;
  22.                    }
  23.                 }
  24.  
  25.             }
  26.         }
  27.     while (validInput == 2);
  28. }
Jul 30 '07 #1
9 1864
Banfa
9,065 Expert Mod 8TB
The & on line 13 is not required buff is alread a pointer to char (char *) which is what is required.

Using %s in scanf is extremely dangerous as there is no buffer overrun protection.
Jul 30 '07 #2
JosAH
11,448 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1.     printf("\nPlease enter purchase amount:", stdout);
This is so very wrong while harmless in this particular case ...
Check your C standard library and check Banfa's reply for the cause of your error.

kind regards,

Jos
Jul 30 '07 #3
fperri
88
The & on line 13 is not required buff is alread a pointer to char (char *) which is what is required.

Using %s in scanf is extremely dangerous as there is no buffer overrun protection.

What would you suggest using?
Jul 30 '07 #4
fperri
88
This is so very wrong while harmless in this particular case ...
Check your C standard library and check Banfa's reply for the cause of your error.

kind regards,

Jos
Why is it so very wrong?
Jul 30 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
What would you suggest using?
Use a getchar(). Then you can call IsDigit() on the character and if it is a digit, you can add it to a buffer. When the numbers are entered, add a \0 the buffer and then use atoi() to get your int.
Jul 30 '07 #6
fperri
88
Use a getchar(). Then you can call IsDigit() on the character and if it is a digit, you can add it to a buffer. When the numbers are entered, add a \0 the buffer and then use atoi() to get your int.
Doesn't the getchar() only grab one character? I need to be able to grab a number like 125.25 if that's what they enter.
Jul 30 '07 #7
fperri
88
Doesn't the getchar() only grab one character? I need to be able to grab a number like 125.25 if that's what they enter.
Why is it so hard to find information on validating user input in C? I just want to be able to only take in a number and have the program keep prompting for the number until a valid number is entered. I cannot find a decent example anywhere online. I can find all the functions for input, but no real examples using it how I want to. Is it really this difficult in C or is it just me?
Jul 30 '07 #8
Banfa
9,065 Expert Mod 8TB
I would use fgets. This reads from a stream but you can use the identifier stdin to read from standard in.

The advantage of this function is that it takes the buffer size so you can eliminate the possibility of buffer overrun errors.

Having read a string into the buffer I would then call strtol (or strtoul), these function convert a string of digits to a long value, however the advantage of these over others is that they return a point to the place the conversion stop so you can see what caused the conversion to halt thus allowing you to fairly easily validate your input as a number.


Don't worry you are not the only person having trouble with this sort of thing, we see it all the time on the forum.
Jul 31 '07 #9
weaknessforcats
9,208 Expert Mod 8TB
Why is it so hard to find information on validating user input in C? I just want to be able to only take in a number and have the program keep prompting for the number until a valid number is entered. I cannot find a decent example anywhere online. I can find all the functions for input, but no real examples using it how I want to. Is it really this difficult in C or is it just me?
Both C and C++ avoid making assumptions. In this case, you are expected to write a function that prompts for data and returns your number. Once you have got that working, you put it in it's own source file, you write a header file for it and you use it for the rest of your life.

Input acquisition and validation are not simple. Requirements vary from program to program so it's really not possible to provide a solution for a specific case. There may not be a keyboard. The number may be in a different number system. The prompt may not apply to all programs, etc.
Jul 31 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Hongzheng Wang | last post by:
In K&R, they said: An object is a named region of storage; an lvalue is an expression refer to an object. How about these concept in C++? `The C++ Programming Language' has a similar...
19
by: Lucas Machado | last post by:
i'm doing some Linux Kernel hacking for a course i'm currently taking. there is a pointer to a struct (struct example_struct *ex_ptr) in a .c that i want to access in a system call. i defined a...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
24
by: Romeo Colacitti | last post by:
Hi, Does anyone here have a strong understanding for the meanings of the terms "lvalue" and "rvalue" as it pertains to C, objects, and different contexts? If so please share. I've been...
44
by: Agoston Bejo | last post by:
What happens exactly when I do the following: struct A { int i; string j; A() {} }; void f(A& a) { cout << a.i << endl;
4
by: seb666fr2 | last post by:
hello, Anybody can tell me why it is impossible to use the '&' operator with operands of type 'long' or 'ulong' and what i must use instead of this? thanks.
22
by: viza | last post by:
Hi all, A quick one - since errno is a lvalue, can I do: fread( & errno, sizeof errno, 1, fp ) ? TIA viza
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.