Thread: Assign string to variable?

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    36

    Assign string to variable?

    How do u assign a string to a character variable?

    For example:

    char question[200];

    question=<this is the string>;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    // initialise
    char question[200] = "This is the question";
    
    // assignment
    strcpy( question, "who knows the meaning of life?" );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    int main()
    {
    int number;
    char question[200];
    char answer[50];
    char correct[50];
    
    srand(time(0));
    number=rand()%5;
    
    if(number==0)
       question="What is The Capital City Of Japan?";
       correct="Tokyo";
    if(number==1)
       question="What is The Capital City Of Philippines?";
       correct="Manila";
    if(number==2)
       question="What is The Capital City Of Mexico?";
       correct="Mexico City";
    if(number==3)
       question="What is The Capital City Of China?";
       correct="Beijing";
    if(number==4)
       question="What is The Capital City Of Brazil?";
       correct="Rio De Janeiro";
    if(number==5)
       question="What is The Capital City Of Canada?";
       correct="Ottawa";
    This is the beginning of my program. It's a small trivia. I'm getting errors on my "if" loops.

    What's wrong with it? Strings are suppose to be enclosed in " " right?
    Last edited by 98dodgeneondohc; 04-24-2005 at 10:32 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    They're also meant to be enclosed in strcpy calls
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    Quote Originally Posted by Salem
    They're also meant to be enclosed in strcpy calls
    why?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Mine works, your's doesn't - you figure it out.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    Quote Originally Posted by Salem
    Mine works, your's doesn't - you figure it out.
    uhhhhh ok. woke up on the wrong side of the bed? if u don't feel like helping, then just simply stop posting in this thread.
    Last edited by 98dodgeneondohc; 04-24-2005 at 10:58 AM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    He did help if you actually pay attention. You asked how to assign something to an array, he showed you. You ignored it and tried it your way, and are scratching your head as to why it doesn't work. Well, the answer is that you were already shown how to do it, but you apparently didn't like it as an answer. Well that's too bad, unfortunately for you, because you can't make assignments with the = operator.

    Once again, you were shown how already.

    Furthermore, your "if" statements are wrong, because you're not enclosing the statements in braces, which yields you a single expression coupled with the if, instead of what I assume you're trying to do by simply indenting.

    Indentation means nothing other than readability. If you want more than one thing to happen in the contest of an if, then enclose all the statements you want to happen in a pair of braces.

    At any rate, pay attention next time and you'll learn more sooner.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM