Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translating an integer and a character into several forms in C

Tags:

c

integer

In my program I have to:

  1. Ask a user for a number and then display that number, then display it as a decimal, an unsigned octal, a hexadecimal, a floating point, a float to two decimal places, then float to three decimal places, a floating point with a signed e and E notation, and a string.
  2. Do the same with a character.

I have just been introduced to strings, so I'm not asking about that here. Just show me what in the name of Opera I could be missing?

Since I revised the code, I am able to get results for everything except '%e' and '%o' when I am defining it by a character.

Here's my original code:

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

int usernumber;

int main(void)
{
    int usernumber;
    puts("Please input a number of your choice:");
    scanf("%d", &usernumber);
    printf("%d %i %f %.2f %.3f %e %E", &usernumber);

    return 0;
}

Here's my new code (revised on 2014-10-28):

int  usernumber ;
char usercharacter;

int main(void) 
{
   int (usernumber);
   int h1 = usernumber;
   int o1 = usernumber;
   int E1 = usernumber;
   int e1 = usernumber;
   char c1 = usernumber;
{
    puts("\n\n------------>Please input a number of your choice:");
    scanf( "%d",&usernumber);
    printf("%d %i %f %.2f %.3f ", usernumber, usernumber, (float) usernumber, (float) usernumber, (float) usernumber);
    printf(" %c"), c1;
    printf(" %X ", h1);
    printf(" %o ", o1);
    printf(" %E ", E1);
    printf(" %e \n ", e1);

}
   char (usercharacter);
   int h2 = usercharacter;
   int o2 = usercharacter;
   int E2 = usercharacter;
   int e2 = usercharacter;
   char c2 = usercharacter;
{
    puts("\n------------>Please input a character of your choice:");
    scanf( " %c",&usercharacter);
    printf("%d %i %f %.2f %.3f", usercharacter, usercharacter, (float) usercharacter, (float) usercharacter, (float) usercharacter);
    printf(" %c"), c2;
    printf(" %X ", h2);
    printf(" %o ", o2);
    printf(" %E ", E2);
    printf(" %e \n ", e2);

}

}

(10.30.14)

I have updated my code once again, creating it in a completely new format. Now I am having trouble from the second function carrying the float of the first. The assignment is already late and I will be having points removed for this anyway, however I am still perusing the solution out of curiosity. If anyone has any advice or an explanation for why this may be happening please feel free to comment!

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



int main(void)
{
char user_input[4200];       // The character string used to pull my integers from.
    {
    int input2integer;
    float input2float;
    puts("\n*----> Please input a number of your choice:  <----*");
    system("COLOR 2");
    scanf("%s", user_input);
    input2integer = atoi(user_input);  // Used to translate the character input into an integer.
    input2float = atof(user_input);    //Used to translate the character input into a float.
    printf("Your input was %s\nWhich could be interpreted in the following ways:\n", user_input);
    printf("int = %i\n", input2integer);
    printf("dec = %d\n", input2integer);
    printf("oct = %o\n", input2integer);
    printf("hex = %X\n", input2integer);
    printf("float = %f\n", input2float);
    printf("two digit float = %.2f\n", input2float);
    printf("three digit float = %.3f\n", input2float);
    printf("e float = %e\n", input2float);
    printf("E float = %E\n", input2float);
    printf("char = %c\n", input2integer);
    printf("string = %s\n", user_input);
    }

       char user_input_2[4200];
       {
       int input2integer_2;
       float input2float_2;
       puts("\n*----> Please input a character of your choice: <----*");
       system("COLOR 2");
       scanf("%s", user_input_2);//scans for a string and throws the value into the input array
       input2integer_2 = atoi(user_input_2);//converts the input array into an integer
       input2float_2 = atof(user_input_2);//converts the input array into a float
       printf("The Character selected was was %s\nThe Following are equivalent:\n", user_input_2);
       printf("int = %i\n", user_input_2);
       printf("dec = %d\n", user_input_2);
       printf("oct = %o\n", user_input_2);
       printf("hex = %X\n", user_input_2);
       printf("float = %f\n", input2integer_2);                 // Unfortunately, despite taking extra time on this,
       printf("two digit float = %.2f\n", input2integer_2);       // I failed to prevent this function from carrying the float from the one before.
       printf("three digit float = %.3f\n", input2integer_2);
       printf("e float = %e\n", input2integer_2);
       printf("E float = %E\n", input2integer_2);
       printf("char = %c\n", user_input_2);
       printf("string = %s\n", user_input_2);
       }
return (0);

(11.1.14)

This is the final, fully working version of this program!

int main(void)
{
char user_input[4200];       // The character string used to pull my integers from.
    {
    int input2integer;
    float input2float;
    puts("\n*----> Please input a number of your choice:  <----*");
    system("COLOR 2");
    scanf("%s", user_input);
    input2integer = atoi(user_input);  // Used to translate the character input into an integer.
    input2float = atof(user_input);    //Used to translate the character input into a float.
    printf("Your input was %s\nWhich could be interpreted in the following ways:\n", user_input);
    printf("int = %i\n", input2integer);
    printf("dec = %d\n", input2integer);
    printf("oct = %o\n", input2integer);
    printf("hex = %X\n", input2integer);
    printf("float = %f\n", input2float);
    printf("two digit float = %.2f\n", input2float);
    printf("three digit float = %.3f\n", input2float);
    printf("e float = %e\n", input2float);
    printf("E float = %E\n", input2float);
    printf("char = %c\n", input2integer);
    printf("string = %s\n", user_input);
    }

       char user_input_2[4200];
       {
       int input2integer_2;
       double input2float_2;

       puts("\n*----> Please input a character of your choice: <----*");
       system("COLOR 2");
       scanf("%s", user_input_2);
       input2integer_2 = atoi(user_input_2);
       input2float_2 = atof(user_input_2);
       input2integer_2 = user_input_2[0]; // *THESE* two lines of code caused me more trouble than everything I have done in this class combined.
       input2float_2 = input2integer_2;   // After a few very sleepless nights, and a massive migraine from staring at screens, I figured them out.
       printf("The Character selected was was %s\nThe Following are equivalent:\n", user_input_2);
       printf("int = %i\n", input2integer_2);
       printf("dec = %d\n", input2integer_2);
       printf("oct = %o\n", input2integer_2);
       printf("hex = %X\n", input2integer_2);
       printf("float = %f\n", input2float_2);
       printf("two digit float = %.2f\n", input2float_2);
       printf("three digit float = %.3f\n", input2float_2);
       printf("e float = %e\n", input2float_2);
       printf("E float = %E\n", input2float_2);
       printf("char = %c\n", user_input_2[0]);
       printf("string = %s\n", user_input_2);
       }
return (0);
}
like image 407
Zach Ross Avatar asked Dec 08 '22 06:12

Zach Ross


1 Answers

Format specifiers only make sense if you provide an argument to printf for each specifier.

If you have 8 format specifiers in the printf string, you need 8 occurences of usernumber in the argument list.

printf("%d",    usernumber);
printf("%d %i", usernumber, usernumber);
...

Also, printf doesn't use the address of its arguments like scanf. It doesn't have to write to them. So remove the & as I showed above for printf.

Keep in mind, that you are hinting to printf what sort of variable you are passing. Because printf() uses variable argument lists (untyped lists), if you pass it something incompatible with the format specifier hints (int for a %f or a variable of the wrong size), the output may be wrong. Consider casting the argument, or moving to a temporary variable. (Regarding casting, keep in mind it only produces a converted value, it doesn't change the original variable)

like image 150
codenheim Avatar answered Dec 28 '22 10:12

codenheim