0

I Have a script c++
but i running this script

This script error when i write code shirt "GC" or "PR"

but i write code "A" this script working

why ?

whether if / else if condition only 1 character ?

This is my script, help me. Thank's

#include<stdio.h> #include<conio.h> #include<iostream.h> main() { char code,size,merk[15]; long price=0; clrscr(); cout <<"\n \t GUCCI : GC"; cout <<"\n \t PRADA : PR \n"; cout <<"\n \t Armani : A"; cout <<"\n"; cout<<"Choose Your Shirt : ";cin>>code; cout<<"Size : ";cin>>size; if (code=='GC' || code=='gc') { strcpy(merk,"Gucci"); if (size=='S' || size =='s') price=45000; else if (size=='L' || size =='l') price=35000; else if (size=='M' || size =='m') price=45000; else price=60000; } else if (code=='PR' || code=='pr') { strcpy(merk,"PRADA"); if (size=='S' || size == 's') price=65000; else if (size=='L' || size =='l') price=75000; else if (size=='M' || size =='m') price=85000; else price=75000; } else if (code=='A' || code=='a') { strcpy(merk,"ARMANI"); if (size=='S' || size == 's') price=75000; else if (size=='L' || size =='l') price=95000; else if (size=='M' || size =='m') price=45000; else price=35000; } else { cout<<"Wrong Code"<<endl; } cout<<"------------------------"<<endl; cout<<"Merk : "<<merk<<endl; cout<<"Price : "<<price<<endl; getch(); } 
3
  • 3
    The term "script" is usually reserved for interpreted languages. Commented Oct 10, 2014 at 0:48
  • 1
    Hint: sizeof(char)==1. std::string could be helpful. Commented Oct 10, 2014 at 0:49
  • I confused, i'm new learn in C++ Commented Oct 10, 2014 at 0:59

2 Answers 2

2

code=='GC' single quotes are for single characters (which "GC" is not). Use double quotes for "strings" (but remember you can't compare strings with ==)...

Since you only read one character at a time, how did you expect to enter "GC"?

Sign up to request clarification or add additional context in comments.

1 Comment

Of course you can compare strings with ==! Since the author is apparently new to the language, mentioning string should be mandatory in order to pass some new knowledge.
0

John3136 is right. You might want to simply the code to tolower() on your input ("code"). All you would need to do is compare to the lower case. Using switch statement, instead of all those if statements, eould make your code more readable.

For comparing multiple characters, use either string, strcmp or stricmp.

1 Comment

i don't understand give me example plis

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.