1
#include <stdio.h>
#define MAX_SIZE 20

int main()
{
  int age;
  char name[MAX_SIZE];
  float wage;

  printf("input name, age and wage: ");

  scanf("%s %s %s", name, &age, &wage); //LINE 10
}

after trying to compile:

lab4q5.c: In function 'main':
lab4q5.c:10: warning: format '%s' expects type 'char *', but argument 3 has type 'int*'
lab4q5.c:10: warning: format '%s' expects type 'char *', but argument 4 has type 'float *'
lab4q5.c:16: warning: control reaches end of non-void function

Im new to C programming and want to scan a float and int but as a String for one of my labs. I know i can change the %s %s to %f %d and it should compile fine, but I'm being asked to scan if as a %s, any help would be much appreciated: the part of the question I'm having trouble with is below:

use loop to read inputs (from standard in), one input per line, about the user information in the form of name age wage, where age is an integer literal, and wage is a floating point literal with up to 2 decimal number. • use scanf(“%s %s %s”, name, age, wage) to read in three input ‘strings’;

so after reading the comments ive decided to format them as strings and then worry about manipulating them as int and float later, this is what I have so far:

#include <stdio.h>
#define MAX_SIZE 20
#define MAX_AGE 3
int isXX(char name[])
{
if (name[0] == 'X' && name[1] == 'X' && name[2] == '\0')
return 0; //return return false to quit
else 
return 1; //return true 
}

int main()
{

char age[MAX_AGE];
char wage[MAX_SIZE];
char name[MAX_SIZE];

printf("input name, age and wage: ");//enter name as XX to quit
scanf("%s %s %s", name, age, wage);
    while(isXX(name[])) // //LINE 22
    {
        printf("input name, age and wage: ");//enter name as XX to quit 
        scanf("%s %s %s", name, age, wage);
    }
return 0;
}

now im not sure why im getting this error in compiler but i am

lab4q5.c: In function âmainâ:
lab4q5.c:22: error: expected expression before â]â token
Athanassios
  • 45
  • 1
  • 6
  • 1
    possible duplicate of [Scanf for a two strings and a float number.](http://stackoverflow.com/questions/7734530/scanf-for-a-two-strings-and-a-float-number) – Adrian Panasiuk Jun 12 '13 at 15:04
  • 1
    If you want them to be read as string, then also define them as string, e.g. `char wage[MAX_SIZE];` – Shahbaz Jun 12 '13 at 15:04
  • wage is a floating point literal, age is an integer literal.. That doesn't mean that the type of age is int and type of wage is float but in my opinion it only means that the entered inputs are in the form of float and int literals.... after scanning age and wage you can convert them to float and int respectively – pinkpanther Jun 12 '13 at 15:07
  • you are correct, but the thing is I am asked to manipulate the numbers after, so wouldn't it be correct to have them of type int and float? or would inputting them as strings then casting them to int and float be a better idea? if scanning them how I am trying to is not possible in C. – Athanassios Jun 12 '13 at 15:09
  • @user2469732 you cannot cast from string to int or float correctly...so for solution...see my answer..... – pinkpanther Jun 12 '13 at 15:16

3 Answers3

5

Use atoi and atof functions to convert from string to int and float respectively. You can first read age and wage as strings and use those functions to convert from string to the respective types and assign them to corresponding new variables.

By the way do not forget to include stdlib.h in order it be working properly.

Corrected Version of your code:

#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 20

int main()
{
char age[MAX_SIZE];
char name[MAX_SIZE];
char wage[MAX_SIZE];
int ageInteger;
float wageFloat;
printf("input name, age and wage: ");
scanf("%s %s %s", name, age, wage); //LINE 10

ageInteger=atoi(age);
wageFloat=atof(wage);
printf("Age:%d, Wage:%f\n",ageInteger,wageFloat);

return 0;
}

Hope this helps...

EDIT:

One comment points out that atoi, atof has undefined behaviour in erroneous cases and also suggests to use stdtol and stdtod instead of atoi, atof. I myself don't know about this. So, take this as a caution before using them and chose carefully chose what you want to do by some research.

pinkpanther
  • 4,770
  • 2
  • 38
  • 62
  • I don't see any literals except format strings. And you can `scanf` for the conversion. – Sebastian Mach Jun 12 '13 at 15:20
  • @phresnel I was just trying to help out this particular problem...sorry if I have used the terms loosely...after all I'm a beginner.....:) please explain me clearly so that I can improve the answer.....:) Oh well that's all I know... the way I solve it...:) if there is a better Idea please post a new answer.... thanks any way – pinkpanther Jun 12 '13 at 15:23
  • A literal is something like `1.5` <-- Literal of type double, or `true` <-- Literal of type `bool`. The format string you pass to `printf` and `scanf` can take different elements, e.g. `%d` means "decimal" and boils down to an integer, `%s` is for strings, `%f` is for floating point numbers. – Sebastian Mach Jun 12 '13 at 15:26
  • @phresnel I know this one...:) in the question `scanf(“%s %s %s”, name, age, wage) ` is restricted...... that's the reason why I gave this solution... once read the quoted text in the question please – pinkpanther Jun 12 '13 at 15:27
  • thanks for the input, Ive edited my original post to show my updated code now, ill figure this out one step at a time :D – Athanassios Jun 12 '13 at 15:31
  • @user2469732 the error in your code is you have to say `isXX(name)` but not `isXX(name[])` – pinkpanther Jun 12 '13 at 15:33
  • @pinkpanther: Ah, sorry for questioning your format string knowledge. I did not read the question carefully enough. – Sebastian Mach Jun 12 '13 at 15:39
  • You should never use `atoi()` or `atof()` unless you're absolutely, utterly, _100%_ certain that there's no possibility of error. The reason for this is that the `atoi()` and `atof()` functions, by definition, invoke Undefined Behavior in the case of error. A better option might be the `strtol()` and `strtod()` functions, which have well-defined error handling. – This isn't my real name Jun 13 '13 at 16:30
  • thanks for the information :) I will keep it in mind and subsequently edit the answer...:) but don't `strtol` and `strtod` hide the error? – pinkpanther Jun 13 '13 at 16:35
3

You are reading them as a string and assigning them to non-string variables.

So you either need to change the code from:

 int age;
 char name[MAX_SIZE];
 float wage;

to:

 char age [3];
 char name[MAX_SIZE];
 char wage[MAX_SIZE];

or

scanf("%s %s %s", name, &age, &wage)

to

scanf("%s %d %f", name, &age, &wage)

Based on the comments to your question, my question is why would you want them as strings to begin with?

Joe Tyman
  • 1,417
  • 5
  • 28
  • 57
  • because for my lab assignment I am being asked to scan them as 3 strings %s %s %s, I have edited my post to show my modified code, now im getting another error – Athanassios Jun 12 '13 at 15:30
  • @user2469732 `name[]` needs to be just `name` or have an index like `name[1]`. You could tell that the error is at the `]` it is looking for an index and not getting one. – Joe Tyman Jun 12 '13 at 19:35
2

You're trying to read in a string but assigning the value to a non-string.

Try to use the right type in the scanf format string:

scanf("%s %d %f", name &age &wage);

You can have a look at how scanf format strings are used on wikipedia

brice
  • 24,329
  • 7
  • 79
  • 95