1

How can I read six digits separately, and then append them?

For example:

I want to enter the following digits: 2 3 6 , 7 5

And the expected output would be: "236,75".

I have to do it with only one loop (to read the numbers), and I have to read the numbers with the type char.

Here's what I have so far:

#include <stdio.h>

int main()
{
    char c;
    char string [6];

    printf("Introduce a number\n");

    int i = 0;
    while (i <=5) {
        scanf("%c", &c);
        string[i] = c;
        i++;
    }

    printf("%c\n",string);

}
  • add string[i] = c before i++ – lightbringer Nov 04 '13 at 00:14
  • Your last printf shows only a character, you must use `%s` to see the whole number stored in array. – Alejandro Sazo Nov 04 '13 at 00:17
  • First, it would help if you said what happens when you try your program. What errors have you encountered? Second, near the end of the code you show, you aren't printing out an item where the `%c` format fits. You're (trying) to print a string, so you want `printf("%s\n", string);`. Finally, is this homework? You appear not to know C very well, but you say you "have to" do certain things. – Telemachus Nov 04 '13 at 00:18
  • @dcarou: I've removed some unnecessary whitespace from your code and added indentation to make it more readable. – Bill Lynch Nov 04 '13 at 00:19
  • @ZachStark: Some countries use the comma to mark decimals. [See wikipedia](https://en.wikipedia.org/wiki/Decimal_mark#Countries_using_Arabic_numerals_with_decimal_comma). – Bill Lynch Nov 04 '13 at 00:21
  • @sharth: Regardless of whether it is used to mark decimals, a comma still isn't a digit. – Zach Stark Nov 04 '13 at 00:23
  • @ZachStark i want to the read 3 numbers, then a comma, and the 2 more numbers –  Nov 04 '13 at 00:28
  • @Telemachus this isn't homework, it's just an exercise i'm trying to do. I'm new to the language, i managed to do it in java, but i'm struggling to do it in C. –  Nov 04 '13 at 00:33

3 Answers3

1

Try something like this:

char string [7];

printf("Introduce a number\n");

int i = 0;

while(i <=5){

    scanf("%c", &c);
    string[i] = c;
    i++;
}

string[i] = '\0';

//printf("%s", string);

I added the '\0' character at string[6], just in case you need that for printing the values for example.

Also, I recommend you to read about cleaning the input buffer when obtaining input from stdin. Hope it helps.

Community
  • 1
  • 1
Cacho Santa
  • 6,846
  • 6
  • 41
  • 73
  • with the imput i entered above, it does not print what it's supposed to –  Nov 04 '13 at 00:25
  • read about [cleaning the input buffer](http://stackoverflow.com/questions/7898215/how-to-clear-input-buffer-in-c). That is your problem now. – Cacho Santa Nov 04 '13 at 01:28
0

For starters ensure that your int main() is actually returning an integer.

#include <stdio.h>

int main()
{
    char c;
    char string[7];
    char* stringy = string;

    printf("Introduce a number\n");

    int i = 0;
    while (i <=5) {
        scanf("%c", &c);
        string[i] = c;
        i++;
    }
    string[i] = '\0';

    printf("%s\n",stringy);

    return 0;

}

Also you want to print your entire string out with a %s format specifier. This also means that you'll need to null terminate your string which can be done by setting the last character in your array to \0.

Also make sure your array has 7 indexes instead of 6, so everything can fit.

turnt
  • 3,235
  • 5
  • 23
  • 39
0
#include <stdio.h>

int main()
{
    char c;
    char string [7];

    printf("Introduce a number\n");

    int i = 0;
    while (i <=5) {
        scanf("%c\n", &c);
        string[i] = c;
        i++;
    }
    string[i] = '\0';

    printf("result: %s\n",string);
    return 0;
}

C strings end with a null character, that is '\0'. Therefore you should always append the null character to a character string as the last character since it determines the end of the string. Therefore the line string[i] = '\0'; is necessary (at the end of the while loop, the value of i will be 6, thus pointing to the last element of the character array).

Another correction to your code is the final printf(). You specified the format to be character format ("%c"), which will only output the first character of the string. You should change it to "%s" for printing the whole string.

This program should work since it is a valid C code. If you are having troubles with this code, then it is platform specific. I couldn't run this code on VS2012, but managed to run it on a GNU C Compiler. Try running the code on an online C compiler, it should work just fine.

Varaquilex
  • 3,447
  • 7
  • 40
  • 60