Answer the question
In order to leave comments, you need to log in
How to read multiple variables in C language?
Hello!
I am now solving programming problems, learning. I came across a task in which 3 variables are read: width, height, depth.
I read them like this:
data_input = scanf("%i %i %i", &width, &height, &depth);
Answer the question
In order to leave comments, you need to log in
Do I understand correctly that the user enters from 1 to 3 numbers per line and expects the program to process these numbers and display the result? In other words, does the program respond to the enter key?
You can read characters one by one with getch() until you read '\n'. You need to parse numbers along the way. If the symbol is a digit, then multiply the current number by 10 and add the digit (remember that (int)'0' != 0. But the digit symbols, thank God, are consecutive in the alphabet). If you read a space - go to the next number. Or, to handle all sorts of lots of spaces, extra characters and other input problems, it's better to read the entire line at once through fgets() and then parse numbers in it.
Think about the user of your program! User is an idiot! And you make riddles for him, demanding to enter something incomprehensible.
First, the user must be warned that input is now expected from him. Secondly, initialize the area and if there is zero (nothing was entered), give an error and re-request
int a = 0, b = 0, c = 0;
for(;;)
{
printf("Введите ширину, высоту и глубину: "); // printf - чтобы перевода строки не было
scanf("%d %d %d", &a, &b, &c);
if ((a) && (b) && (c)) // сокращенная форма записи if (a != 0) ...)
break;
else
printf("Неверно, попробуйте еще раз\n");
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question