How do u assign a string to a character variable?
For example:
char question[200];
question=<this is the string>;
How do u assign a string to a character variable?
For example:
char question[200];
question=<this is the string>;
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.
This is the beginning of my program. It's a small trivia. I'm getting errors on my "if" loops.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";
What's wrong with it? Strings are suppose to be enclosed in " " right?
Last edited by 98dodgeneondohc; 04-24-2005 at 10:32 AM.
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.
why?Originally Posted by Salem
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.
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.Originally Posted by Salem
Last edited by 98dodgeneondohc; 04-24-2005 at 10:58 AM.
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.
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.