简体   繁体   中英

while loops , int vs char reading from scanf , true and false

#include <stdio.h>
int main()
{
    int number; // We make 1 int named number
    scanf("%d",&number); // We give to number some number xd
    while (number!=0)// We made a loop while if number isn't 0 do that
    {
        printf("text");// print text
        scanf("%d",&number); // and get again a number. 
        // So everything works well beside inserting some char instead of int.
        // So what is the problem wont scanf return 0 so we exit the program not
        // just printing a text all day?  That's my first question.
    }
    return 0;
}

The second problem is how to make a program reading numbers from keyboard till I enter some special sign for ex '.' Yea we do it with loop while right? But how when scanf("%d",&something) it give me back 0 if I enter everything but number?

change it from scanf int to char

Assumption: You are reading a single char at a time

#include <stdio.h>

int main()
{
    char c = "0";
    int n = 0;

    while (n != 46)// we made a loop while if char isn't '.' ASCII - 46 do that
    {
        scanf(" %c", &c);
        n = (int)c;
        printf("text %d", n);
        //Add if statement to confirm it is a number ASCII 48 - 57 and use it.
    }
    return 0;
}

EDIT: Just some more info on how to use numbers: input number one by one or just a whole number like 123 with some ending special char like ';' or change the line

scanf(" %c", &c);

to:

scanf("%c", &c);

this way it will register '\n' as ASCII 10

use that with atoi to get the actual int value and use it.

EDIT 2:

@World, you cannot expect to read only numbers and '.' One way to do this with your own code is:

#include <stdio.h>

int main()
{
    char c = "0";
    int n = 0;
    int number = 0;

    while (n != 46)// we made a loop while if char isn't '.' ASCII - 46 do that
    {
        scanf("%c", &c);
        n = (int)c;

        if (n>=48 && n<=57) {
            number *= 10;
            number += (n-48);
        }
        if (n==10) {
             printf("text %d\n", number);
             //Use number for something over here
             number = 0;
        }
    }
    return 0;
}

EDIT 3: Logic behind this:

Let's say you enter 341 in the console the line

scanf("%c", &c);

will read this one by one thus reading '3', '4', '1' and '\n' respectively the while loop

while (n != 46)

will thus run 4 times where:

1st time

c = '3';
n = 51;
if (n>=48 && n<=57) is true;
number = 0 * 10;
number = number + n - 48 = 0 + 51 - 48 = 3

2nd time

c = '4';
n = 52;
if (n>=48 && n<=57) is true;
number = 3 * 10 = 30;
number = number + n - 48 = 30 + 52 - 48 = 34

3rd time

c = '1';
n = 49;
if (n>=48 && n<=57) is true;
number = 34 * 10 = 340;
number = number + n - 48 = 340 + 49 - 48 = 341

4th time

c = '\n';
n = 10;
if (n>=48 && n<=57) is false;
if (n==10) is true;
it prints "text 341" and sets number to 0

while loop exits when c = '.'and n = 46

how to make a program reading numbers from keyboard till I enter some special sign for ex '.'

When scanf("%d",&number) returns 0, there is some non-numeric input. Read that non-numeric input with alternate code.

#include <stdio.h>

int main() {
  while (1) {
    int number;
    int scan_count = scanf("%d", &number);
    if (scan_count == EOF) break;  // End-of-file or rare input error.
    if (scan_count != 1) {
      int ch = fgetc(stdin);
      if (ch == '.') break;  // Expected non-numeric input to break the loop.
      printf("Unexpected input character '%c'\n", ch);
    } else {
      printf("Expected numeric input: %d\n", ch);
    }
  } // endwhile
  printf("Done\n");
}

A better approach is to ditch scanf() and use fgets()` to read user input. Then process the inputted string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM