Find the answer to your Linux question:
Results 1 to 6 of 6
HI! In the program I am writing, it gives commands based on what the value of the variable is. In this case all values are completely text (no integers) and ...
  1. #1
    Just Joined!
    Join Date
    Oct 2011
    Posts
    7

    string help

    HI! In the program I am writing, it gives commands based on what the value of the variable is. In this case all values are completely text (no integers) and I can't think of what to use besides if. I have tried for, else, while, ans else-if with no luck. Thank you.

  2. #2
    Just Joined!
    Join Date
    Apr 2011
    Posts
    87
    let me see your code

  3. #3
    Just Joined!
    Join Date
    Oct 2011
    Posts
    7
    //Character Creation
    #include <stdio.h>

    main()
    {
    char name, race, class, age, weapon, area, answer;

    printf("Please write all input as lowercase, except for name and area, and on age please spell the numebr\n");

    //Trial 1
    puts("Choose your name: ");
    scanf("%s", &name);
    puts("Choose your race:\nDwarf\nElf\nHalf-Elf\nHuman\nOrc ");
    scanf("%s", &race);
    puts("Choose your class:\nFighter\nMage\nRanger ");
    scanf("%s", &class);
    puts("What is your age: ");
    scanf("%s", &age);
    puts("Choose your weapon:\nSword\nSpell Book\nBow ");
    scanf("%s", &weapon);
    puts("Where do you live:\nElixia\nDragoda\nThe Temple City ");
    scanf("%s", &area);
    puts("Are all of these correct? ");
    scanf("%s", &answer);

    if(answer == "no")
    puts("Choose your name: ");
    scanf("%s", &name);
    puts("Choose your race:\nDwarf\nElf\nHalf-Elf\nHuman\nOrc ");
    scanf("%s", &race);
    puts("Choose your class:\nFighter\nMage\nRanger ");
    scanf("%s", &class);
    puts("What is your age: ");
    scanf("%s", &age);
    puts("Choose your weapon:\nSword\nSpell Book\nBow ");
    scanf("%s", &weapon);
    puts("Where do you live:\nElixia\nDragoda\nThe Temple City ");
    scanf("%s", &area);
    puts("Are all of these correct? ");
    scanf("%s", &answer);

    if(answer == "yes")
    printf("So %s, you are a %s year old %s %s who lives in %s and uses a %s\n", &name, &age, &race, &class, &area, &weapon);
    }

  4. #4
    Linux Newbie
    Join Date
    Dec 2009
    Posts
    241
    Hi,
    to tell you the truth, I hardly can C.

    Here some errors I can figure you:
    Code:
    char name, race, class, age, weapon, area, answer;
    Will declare these Values are single characters.
    They can just store one digit.

    Code:
    scanf("%s", &name);
    Will store the information into the position where name points.
    So let's assume name has the value 0.
    Since it ain't initialized it can have any value between 0 and 255.
    So you will write a string into the memory assigned to your program position 0.
    You are not changing the value of name.

    You should check out a tutorial for C explaining pointers and arrays!

  5. #5
    Just Joined!
    Join Date
    Oct 2011
    Posts
    7

    thank you

    Thank you this helps. I can't find a site that will teach me how to write in c beyond hello world do you know of any?

  6. #6
    Linux Newbie
    Join Date
    Dec 2009
    Posts
    241
    Hi guess that one is fine for the moment:
    A Tutorial on Pointers and Arrays in C
    Or that one:
    Pointers - C++ Documentation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •