3

I am absolutely brand new at programming and im not sure how to explain what im doing here.

The whole purpose of this piece is to enter values and then print them out in the same order. Now I wanna quit from entering values when pressing 'q' and so I have to scanf for chars but when I assign them back to the int array the values are not the same.

Hope that makes any sense to you but in any case heres my code:

#include <stdio.h>
#include <stdlib.h>
#define SIZE  5000
define flush fflush(stdin)

main() {
    int input[SIZE] = {0},i = 0;
    int counter = 0;
    char inputs, quit;

     do {
        system("cls");
        printf("Input number ('q' to quit and display numbers entered): ");

        flush;
        scanf("%c",&inputs);
        flush;

        if (inputs == 'q')
            quit = 'q';
        else {
            input[i] = inputs;
            counter++;
            i++;
        }
    } while (i < SIZE && quit != 'q');

    for(i = 0; i < counter; i++){
        printf("%i.%i\n", i + 1, input[i]);
    }

    system("pause");
}

Ive been trying to do this on my own btw and also researched some information online regarding chars but couldnt find anything that would help me. Thanks a lot in advance.

  • 1
    Simple: what you're doing is UB. –  Oct 27 '12 at 22:21
  • 4
    `fflush(stdin)` has undefined behavior. Don't do it. – Keith Thompson Oct 27 '12 at 22:22
  • Storing a `char` in an `int` array doesn't convert that character from its ASCII encoding to the decimal result you're hoping. ie. `'1'` is represented in decimal as 49, and that's the value actually being stored in memory. You should instead use the `%d` specifier, which will do this conversion for you. It'll also allow numbers longer than a digit to be read in. You can limit the length of that number with a width specifier if that's necessary. – AusCBloke Oct 27 '12 at 22:32
  • http://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior – Mateen Ulhaq Oct 27 '12 at 22:33

3 Answers3

3

You should nor be getting integer through %c neither assign char values to integers variables when that is not the intention, rather you should approach something like this

i = 0;
do {
  printf("Enter a number: ");
  scanf("%d", &input[i]);
  i++; counter++;
  printf("Do you want to continue? (y/n) : ");
  scanf("%c", &inputs);
} while(inputs == 'y');

or u can get the number of integer inputs upfront and loop to get that much integers.

AnandVeeramani
  • 1,526
  • 9
  • 15
  • He is not passing in an integer... `scanf("%c", &inputs);` is alright since `inputs` is of type `char`. Also, `input[i] = inputs;` is fine too, there's an implicit conversion taking place. –  Oct 27 '12 at 22:29
  • Then why is he assigning "input[i] = inputs;" – AnandVeeramani Oct 27 '12 at 22:31
  • again, implicit type conversion. –  Oct 27 '12 at 22:31
  • if he enters 0 it will be displayed as ASCII of 0, ie, 48. Is that what is expected? – AnandVeeramani Oct 27 '12 at 22:33
  • @AnanVeeramani no, but how is that related? He never compares `inputs` nor `input[i]` against `0`. –  Oct 27 '12 at 22:35
  • See man he is trying to get a input and print the same, but he doesn't distinguish between int and char type, he is reading it and checking it for quiting as well as storing in the int array, but he must understand that he need to use proper data types and its purposes. – AnandVeeramani Oct 27 '12 at 22:41
  • If you don't fix the root cause he is again going start another SO question saying I am input 0 but getting 47 as output – AnandVeeramani Oct 27 '12 at 22:42
  • I see what you mean, but this is not a problem here. In this particular case, type promotion occurs and the integer variable which is assigned to a `char` is simply zero-extended. –  Oct 28 '12 at 05:34
  • @AnanVeernami I just explained it. The error in the code is **not** `int input[5000]; input[i] = 'q';` which you think it is. The problem is `fflush(stdin);`, which you don't write a single word about. –  Oct 28 '12 at 05:39
0

try instead (using your original code as much as possible):

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define SIZE  5000

int main()
{
  int input[SIZE] = {0},i = 0;
  int counter = 0;
  char inputs[32];
  bool quite = false;

  do
  {
    system("cls");
    printf("Input number ('q' to quit and display numbers entered): ");

    // read a string from user, then convert when appropr
    fgets(stdin, sizeof(inputs), inputs); 

    if (inputs[0] == 'q')
    {
        quit = true;
    }
    else if ( isdigit(inputs[0]) )
    {
        input[i] = atoi(inputs); // this will disregard any ending \n
        counter++;
        i++;
    }
  }
  while (i < SIZE && !quit);

  for(i = 0; i < counter; i++)
  {
    printf("%i.%i\n", i + 1, input[i]);
  }
  system("pause");
}
AndersK
  • 35,813
  • 6
  • 60
  • 86
  • So im guessing i have to make the int array to a char array and scanf with %d. Which will then scan for numbers and characters in ascii code and convert to normal. And btw sorry for the late reply but I work nights sometimes and I was unable to check and thanks a lot to everyone for the responses. I could have made this work for my homework assignment since it only required us to input positive values which would have then allowed me to use a -# to quit. But what would be the fun in that. – user1780004 Oct 28 '12 at 16:35
  • I tried to understand most of the things you guys posted but I am having a hard time following up. Like I said im very new to this and been on it for about 4 weeks now. However, I did some changes to my code but now it is giving me an "expression must be a modifiable lvalue" for the "i". I also dont have a book and most of the information is provided by tutorial videos from the school so if you guys have any good sources for learning, I would appreciate you sharing them. – user1780004 Oct 28 '12 at 16:51
  • I am also trying to figure out how to post the code in the comments so you guys can see the changes but Im having trouble with that. This is my first post so please go easy on me :). Thanks. – user1780004 Oct 28 '12 at 16:56
0

Another variant. This one will read in characters regardless of the use of whitespaces, since it uses getchar() rather than scanf(). I'm not sure if this is what you want. It seems as though you want integers but are reading characters. So this solution may be completely off base.

#include <stdio.h>
#include <stdlib.h>
#define SIZE  5000

int main()
{

    char input[SIZE] = {0};
    int i = 0;
    int counter = 0;
    char inputs;

    printf("Input number ('q' to quit and display numbers entered): ");

    while (((inputs = getchar()) != EOF) && (counter < SIZE))
    {
        if (inputs == 'q')
            break;

        input[counter] = inputs;
        counter++;
    }

    for(i = 0; i < counter; i++)
    {
        printf("%c\n", input[i]);
    }

    system("pause");

    return 0;
}

If you do really want ints, this one should work.

Notice that the atoi() function can be used to convert a C-string to an int.

The fgets() function is used to read the C-string from STDIN. However, scanf("%s", input); would also work here, as opposed to the scanf("%c", &inputs); that you used.

#include <stdio.h>
#include <stdlib.h>

#define INPUT_SIZE 1000
#define SIZE  5000

int main()
{

    char input[INPUT_SIZE] = {0};
    int numbers[SIZE] = {0};
    int i = 0;
    int counter = 0;

    while ((fgets(input, sizeof(input), stdin) != NULL) && (counter < SIZE))
    {
        system("cls");
        printf("Input number ('q' to quit and display numbers entered): ");

        if (input[0] == 'q')
            break;

        numbers[counter] = atoi(input);
        counter++;
    }

    for(i = 0; i < counter; i++)
    {
        printf("%i\n", numbers[i]);
    }

    system("pause");

    return 0;
}
Geoff Montee
  • 2,587
  • 13
  • 14