Mukasa Ricketts
Mukasa Ricketts

Reputation: 68

Unable to read in values of type double

Basically the user enters the data and said data is then printed to the screen. Data is entered in the form:

firstName lastName score

The problem with the code is that it doesn't read in the values of type double for the score.

I've tried using different format specifiers but I think the problem may just be something that I just really don't know about at all

#include<stdio.h>
#include<string.h>
#define colN 20
void printRecords(char fName[][colN], char lName[][colN], double score[],int rowN);//#1
int main(){
    int rowN=0,i=0;
    printf("Please input the # of records  you want to enter (min 5, max 15):\n");
    scanf("%d",&rowN);
    char first[rowN][colN],last[rowN][colN];
    char inFirst[colN],inLast[colN];//for input first and last names needed when searching by first/last in records
    double grade[rowN];
    printf("Please input records of students (enter a new line after each record),\n");
    printf("with following format --> first name last name score\n");
    for(i=0;i<rowN;i++){
        scanf("%s",&first[i]);//get firstName
        scanf("%s",&last[i]);//get lastName
        getchar();
        scanf("%f",&grade[i]);//get score
    }

    printf("\n");
    return 0;
}

void printRecords(char fName[][colN], char lName[][colN], double score[],int rowN){//#1
    //First Name: firstname 1, Last Name: lastname 1, Score: score 1
    int i=0;
    for(i=0;i<rowN;i++){
        printf("First Name: %s\tLast Name: %s\tScore: %.2f\n",fName[i],lName[i],score[i]);
    }
}

Expectd out is to print to the screen the user entered firstName, user enterd lastName and user entered score

Upvotes: 0

Views: 55

Answers (1)

cocool97
cocool97

Reputation: 1251

You are trying to read a double as a string with the %f formater. If you want to scan double values, you have to use the %lf formater.

Upvotes: 2

Related Questions