user7604801
user7604801

Reputation: 45

In C how do I detect whether an input is a character or integer at the same time?

Quite new to C so apologies for the long read and no doubt rubbish programming.

So my program is reading in a CSV file, containing the specification of 15 different transistors. It then compares an input to the data from the CSV file. After the data is input, the program determines which transistors are most suitable. This is meant to loop until the letter q is entered, which is when the program is meant to stop.

The input is entered as eg.

10 0.05 120 5000 20

for volt, amp, power, freq and gain respectively. The program doesn't care about type, company or price.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define MAX 15
#define MAX_IN 10000

struct data{
char type[10], company[10];
int volt, power, freq, gain;
float amp, price;
};

struct input{
int volt, power, freq, gain;
float amp;
};
struct input inputs[MAX_IN];
struct data datas[MAX];


main()
{
    int chk = 1, num=0;
    while(chk == 1)
    {
        chk = input_function(&num);
    }
}

int input_function(int *number)
{
    int j=0;
    char check=0;

    scanf("%c%d %f %d %d %d", &check, &inputs[j].volt, &inputs[j].amp, inputs[j].power, &inputs[j].freq, &inputs[j].gain);

    if(check!='q')
    {
        check_function(j);
        ++*number;
        ++j;
        return 1;
     }
     else
     {
        return 0;
     }
}

num is an integer used to determine how many inputs I have put in so that the check function compares the correct input for the input structure.

In a previously similar problem, using %c%d let it check for both a character and an integer at the same time, but this isn't working.

Previously I had it almost working, but it took q as the first input to voltage - so every input after was off by one. Anyone have any ideas?

Upvotes: 2

Views: 126

Answers (1)

zwol
zwol

Reputation: 140569

scanf, fscanf, and sscanf are broken-as-specified and shouldn't ever be used for anything. Forget you ever heard of them.

Use fgets (or getline if available) to read the entire line. Examine its first character manually. If it is q, exit. Otherwise, use a chain of calls to strtol and strtod to parse the line and convert text to machine numbers. (Pay close attention to the part of the strtol/strtod manpages where they explain how to check for errors; it's a little tricky.)

Upvotes: 3

Related Questions