-2

This is a piece of code I'm writing while taking CS50. I'm writing this code in CS50 IDE in C. I want to make it such that I can input a name or a number without having to write seprate code for each, but I don't know how to do that. Any help would be much appreciated. Thank you.

#include<cs50.h>
#include<stdio.h>

int main(void)
{
    printf("Name: \n");
    string name1 = get_string();
    if (name1 is in, int name1 = get_int())
    ;
    printf("Nice, %s\n", name1);
}
Toby
  • 9,696
  • 16
  • 68
  • 132
  • 1
    `if (condition) ;` Is exactly the same as doing anything. Remove the semi-colon. Or may be I misunderstood what you wanted to do, and you should write `if (name1 is in) int name1 = get_int();` – Badda May 04 '17 at 14:44
  • 3
    CS50 has its own stack exchange: https://cs50.stackexchange.com/ – Toby May 04 '17 at 14:55
  • It is not possible to define the same variable name in the same scope. – BLUEPIXY May 04 '17 at 15:03
  • 1
    What is `if (name1 is in, int name1 = get_int())`? – Jabberwocky May 04 '17 at 15:05
  • 3
    Your code isn't (valid) C. I suggest you remove the C tag. – pmg May 04 '17 at 15:07
  • 1
    Your desire _I want to make it such that I can input a name or a number without having to write separate code for each_ is not attainable with current versions of C, unless you store the value in a string unconditionally and then conditionally convert the string to a number if it should be treated as a number. You'll still have to process the values differently after the input. – Jonathan Leffler May 04 '17 at 15:12
  • There is no string type in C. This is not a code. If you want to write a pseudo code, you need to identify it as a pseudo code and describe in English. I still don't understand this project spec. – Nguai al May 04 '17 at 15:15
  • 1
    @Nguaial the `cs50.h` header defines `string` as `char *` (which is of course a terrible idea). – Jabberwocky May 04 '17 at 15:23
  • @MichaelWalz - Thanks! – Nguai al May 04 '17 at 15:25
  • Give us a broader picture: once the user has entered the name or the number what will happen then apart the printf? BTW you may just use `get_string();`, after all a number __is__ a string. – Jabberwocky May 04 '17 at 15:32
  • Will mankind be ever free from this abomination called cs50? – Ajay Brahmakshatriya May 04 '17 at 17:12
  • @MichaelWalz Your advice was perfect. The code works properly. I assumed that a string would only hold a specific of characters but i wrongly assumed that it wouldn't hold numbers, although they are characters. Thank you for your help and to everyone else who answered. My issue was less with code and more with my understanding of strings. – MilkWithSalt May 05 '17 at 08:44

2 Answers2

2

C doesn't natively support type polymorphism such that you can write code that handles both int and char [], nor can you determine the type of an object at runtime.

There are some techniques you can use to fake it. You can use macros or function pointers to create type-agnostic interfaces that defer type-specific handling to other functions. C11 introduced the _Generic macro that allows you to pick an action based on the type of an argument:

#define DO_SOMETHING_WITH( X ) _Generic( (X),                           \
                                         int : do_something_with_int,   \
                                      char * : do_something_with_string \
                                       )( X )

void do_something_with_int( int arg )
{
  ...
}

void do_something_with_string( const char *arg )
{
  ...
}

int main( void )
{
  int x;
  char y[SOME_LENGTH];

  DO_SOMETHING_WITH( x );
  DO_SOMETHING_WITH( y );
  ...
}

but in the end, you still have to write the code that does the type-specific handling.

John Bode
  • 119,563
  • 19
  • 122
  • 198
0

Also if you try to do printf("Nice, %s\n", name1); and name1 is an int you will get an error. You should use printf("Nice, %d\n", name1); for printing integers

Darkpingouin
  • 196
  • 12