Athanassios
Athanassios

Reputation: 45

Scanning floats and ints as a string in C

#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

Upvotes: 1

Views: 9094

Answers (3)

pinkpanther
pinkpanther

Reputation: 4808

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.

Upvotes: 5

brice
brice

Reputation: 25039

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

Upvotes: 2

Joe Tyman
Joe Tyman

Reputation: 1417

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?

Upvotes: 3

Related Questions