0
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
    char names[10][10], name[10], gender;
    printf("Welcome to Autumn sim");
    names = ["Nathan", "August", "Bob", "Joe", "Stewart", "Martha", "Tiffany", "Darlene", "Kate", "Sophie"];

    printf("What is your gender? m or f?\n");
    scanf("%c", &gender);
    if (gender == "m" || "M")
    {
        name = names[rand(0, 4)][10];
    }
    else if (gender == "F" || "f")
    {
        name = names[rand(5, 9)][10];
    }

    printf("Your name is...");
    printf("Loading");

    printf("%s", name);
    return 0;

}

This is my program. I am trying to ask gender and randomly give their name. There are five names if they are male and other five names if they are female. But it keep saying !expression must be identifiable 1 value! which I cannot understand. please somebody help me!!! Also, I want to know how to wait in c, I want to wait after "Loading".

  • 3
    There are many basic issues in your code. C is not Python. You are probably better off reading a proper text book on C. – P.P Nov 07 '16 at 12:50
  • I'm voting to close this question as off-topic because the OP has no understanding of the underlying principals of the language that s/he is attempting to write in – KevinDTimm Nov 07 '16 at 13:44

4 Answers4

2

You can not assign to an array, only copy to it. Or initialize it when you define the array.

The simple solution is to initialize the array:

char names[10][10] = {
    "Nathan", "August", "Bob", "Joe", "Stewart",
    "Martha", "Tiffany", "Darlene", "Kate", "Sophie"
};

You also have a problem with your comparison for e.g. gender. The variable gender is a character and so you need to compare it with another character. And character literals are using single-quotes and not double-quotes. So the character m is 'm' (not "m" which is a string).

Then the logic is wrong as well. The expression gender == 'm' || 'M' is actually equal to gender == ('m' || 'M'), meaning you compare gender to the result of 'm' || 'M'. The expression 'm' || 'M' will always be 1, so the comparison will fail. You need to explicitly compare gender with both 'm' and 'M' as in gender == 'm' || gender == 'M'.

Or you could use the tolower function like tolower(gender) == 'm'.


To get a single name from your array of names, you have two choices. One is to keep using your array name, but since you can assign to an array you have to copy to it. To copy strings one typically uses the strcpy function:

strcpy(name, names[1]);  // Copy the second name

The other possibility is to use pointers, so you declare name as a pointer to char:

char *name;

Then you can make it point to any element in the names array:

name = names[1];  // Make name point to the second name

Which one you should use depends on what you are doing. If you just want to display the string, then either will work fine. If you want to modify the string, without modifying the strings in names then you should use an array.


Lastly there is another problem with your code. The rand function does not work as you expect.

See e.g. this old answer FOr how to generate a random number in a range.

Community
  • 1
  • 1
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

there are a lots of errors in your code...

  • First you use names but don't specify the type.
  • You compare gender, a char (gender) with "m", and double quote means a char * value.
  • You cannot do if (gender == "m" || "M"), that means if the gender is equal to "m", or if "M" have a value (is different of zero), and it is, so the condition will always be true.
  • On this line : name = names[rand(0, 4)][10];, what is the type of name, and what is the type of names[rand(0, 4)][10] ?
  • You should read the man of rand, you don't use it correctly.



Try to fix these issues, this should work better ;)

Thomas Blanquet
  • 507
  • 6
  • 17
0

The problem is in

names = ["Nathan", "August", "Bob", "Joe", "Stewart", "Martha", "Tiffany", "Darlene", "Kate", "Sophie"];

you cannot assign to an "array" type, and the syntax is also wrong.

you have to either

  • assign element by element
  • initialize the array using the brace-enclosed initializer list.
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

There are many ways to achieve this. This is one of the way.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
    char name[10] = "";
    char gender   = ' ';
    printf("Welcome to Autumn sim");
    char names[10][10] = ["Nathan", "August", "Bob", "Joe", "Stewart", "Martha", "Tiffany", "Darlene", "Kate", "Sophie"];

    printf("What is your gender? m or f?\n");
    scanf("%c", &gender);
    if (gender == 'm' || gender == 'M') //Compare gender with charecter ('m' OR 'f')
    {
        strcpy(name, names[rand(0, 4)]); //Copy the string
    }
    else if (gender == 'F' || gender  == 'f')
    {
        strcpy(name, names[rand(5, 9)]);
    }
    else
    {
       printf("Invalid gender entered") //Check if input is invalid
    }

    if(0 < strlen(name)) //Print only if input is valid
    {
        printf("Your name is...");
        printf("Loading");

        usleep(1000*1000*1000); //1s sleep

        printf("%s", name);
    }
    return 0;

}
MayurK
  • 1,925
  • 14
  • 27