ännu ett problem i c++ read if(wantto=yes)

Permalänk

ännu ett problem i c++ read if(wantto=yes)

nu har jag börjat förvanlads till c++'are. så nu pillar jag lite på ett problem med "if". Jag skall genom if ställa en fråga. man skall kunna svara "yes" & "no". om man svara "no" skall en text komma fram, du är bankrupt, om man skriver "yes" skall "okey då kör vi" komma fram, hur gör jag detta nu då..
jag har jälv skrivigt såhärande (men fått fel när jag provar det):

if(q=yes)
{
cout<<"Okey, då kör vi";
}
else
{
cout<<"Du är bankrupt";
}

ps: jag har gjort \x86 osv för å, ä och ö bara så ni vet....

snälla rädda mig...
ps: när spelet är klart läggs det upp på min websajt som open source program där ni proffs kan updatera skiten...

tack i förhand // den taktande loosern

Visa signatur

Våga inte ens prata filosofi med mig...

Permalänk
Medlem

if (q == "yes") cout << "Okey, då kör vi"; else cout << "Du är bankrupt";

Visa signatur

"`F' is group reply. This is because `G' is for sort and `g' is for get new mail. And `G' is for sort because `sort' used be called `group', and anyway `S' is for save mailbox and `s' is for file message in folder, because `f' is group reply and `F' is group reply including original text."

Permalänk

local veriable yes have been used without initaliased...
tack ändå men det funkade int för mig...
iaf inte när jag skrev no till frågan och ville
att "du är bankrupt" skulle komma upp =(

Visa signatur

Våga inte ens prata filosofi med mig...

Permalänk
Medlem

Är q en char array ?
char q[10];

Då måste du använda dig av följande

if( strcmp(q, "yes") == 0) // lr if( !strcmp(q, "yes") ) cout << "Okey, då kör vi"; else cout << "Du är bankrupt";

Visa signatur

"Anyone who puts a small gloss on a fundamental technology, calls it proprietary, and then tries to keep others from building on it, is a thief."
-Tim O'Reilly "http://iiice.net/~ice/stuff/secret_msg.wav" - who?

Permalänk

det funkade inte heller... skall strcmp vara en variabel eller vafan de nu e de skall vara?

Visa signatur

Våga inte ens prata filosofi med mig...

Permalänk
Medlem

strcmp är en funktion som finns i filen string.h (tror jag den hette), alltså glöm inte

#include <string.h>

Visa signatur

Min signatur - inte din!

Permalänk
Medlem

En funktion.. tycker du sak ta och läsa på lite innan du börjar fråga i forum.
Eller som man kan säga:

One day a Novice came to the Master.
"Master," he said, "How is it that I may become a Writer of Programs?"
the Master looked solemly at the Novice.
"Have you in your possession a Compiler of Source Code?" the Master asked.
"No," replied the Novice.
the Master sent the Novice on a quest to the Store of Software.

Many hours later the Novice returned.
"Master," he said, "How is it that I may become a Writer of Programs?"
the Master looked solemly at the Novice.
"Have you in your possession a Compiler of Source Code?" the Master asked.
"Yes," replied the Novice.
the Master frowned at the Novice.
"You have a Compiler of Source. What now can prevent you from becoming
a Writer of Programs?"
the Novice fidgeted nervously and presented his Compiler of Source to
the Master.
"How is this used?" asked the Novice.
"Have you in your possession a Manual of Operation?" the Master asked.
"No," replied the Novice.
the Master instructed the Novice as to where he could find the
Manual of Operation.

Many days later the Novice returned.
"Master," he said, "How is it that I may become a Writer of Programs?"
the Master looked solemly at the Novice.
"Have you in your possession a Compiler of Source Code?" the Master asked.
"Yes," replied the Novice.
"Have you in your possession a Manual of Operation?" the Master asked.
"Yes," replied the Novice.
the Master frowned at the Novice.
"You have a Compiler of Source, and a Manual of Operation. What now can
prevent you from becomming a Writer of Programs?"

At this the Novice fidgeted nervously and presented his Manual of Operations to
the Master.
"How is this used?" asked the Novice.
the Master closed his eyes, and heaved a great sigh.
the Master sent the Novice on a quest to the School of Elementary.

Many years later the Novice returned.
"Master," he said, "How is it that I may become a Writer of Programs?"
the Master looked solemly at the Novice.
"Have you in your possession a Compiler of Source Code, a Manual of Operation
and an Education of Elementary?" the Master asked.
"Yes," replied the Novice.
the Master frowned at the Novice.
"What then can prevent you from becomming a Writer of Programs?"

the Novice fidgeted nervously. He looked around but could find nothing to
present to the Master.
the Master smiled at the Novice.
"I see what problem plagues you." said the Master.
"Oh great master, please tell me." asked the Novice.

the Master turned the Novice toward the door, and with a supportive hand on his
shoulder said, "Go young Novice, and Read The Fucking Manual."

And so the Novice became enlightened.

EDIT: http://www.gametutorials.com/Tutorials/tutorials.htm är en bra sida där man kan läsa.

Visa signatur

"Anyone who puts a small gloss on a fundamental technology, calls it proprietary, and then tries to keep others from building on it, is a thief."
-Tim O'Reilly "http://iiice.net/~ice/stuff/secret_msg.wav" - who?

Permalänk

vafan skall man göra om man inte har någon manual då? plus vad e forum till för ?

tack allihopa..

Visa signatur

Våga inte ens prata filosofi med mig...

Permalänk

haha.....
din mamma skulle vara stolt över dig nu marcus lille!
Ses i plugget imorrn!
// Schnazzen

Visa signatur

Hej hopp!

Permalänk
Medlem

> plus vad e forum till för ?

Att fråga om saker som man inte förstår eller hittar information om på annat håll. I det här fallet skulle du ha kunnat få svaret direkt om du hade läst lite i en C++-bok eller på en C++-skola på internet.

Antingen använder du nullterminerade strängar eller t ex STL-strängar.

1. Nullterminerad sträng:

char q[18];
cin.getline(q,sizeof(q));

if( !strcmp(q,"yes") )
cout<<"yes"<<endl;
else
cout<<"annars"<<endl;

2. STL-sträng

string q;
getline(cin,q);
if(q=="yes")
cout<<"yes"<<endl;
else
cout<<"annars"<<endl;

--------------------------------------

Få med det här längst upp också:

#include <iostream>
#include <string> //edit: för STL-strängar
using namespace std;

Visa signatur

//cykze

Permalänk

cykze. detta funkar inte heller för mig....

Visa signatur

Våga inte ens prata filosofi med mig...

Permalänk
Medlem

Vad är det som inte fungerar? Felmeddelanden...

Glömde förresten att det ska vara en #include <string> i STL-exemplet.

Visa signatur

//cykze

Permalänk

den hade jag med... stringen alltså. Felmeddelandet klydda med:
"getline"

Visa signatur

Våga inte ens prata filosofi med mig...

Permalänk
Medlem

Kör du med STL-varianten nu?

Visa signatur

//cykze

Permalänk

jepp

Visa signatur

Våga inte ens prata filosofi med mig...

Permalänk
Medlem

#include <iostream>
#include <string> //glömde igen
using namespace std;

void main()
{
string test;
getline(cin,test);

if(test=="yes")
cout<<"yes"<<endl;
else
cout<<"annat"<<endl;
}

fungerar det?

Visa signatur

//cykze

Permalänk

skall kolla....

Visa signatur

Våga inte ens prata filosofi med mig...

Permalänk

test.cpp(7) : error C2065: 'getline' : undeclared identifier
test.cpp(9) : error C2065: 'yes' : undeclared identifier
test.cpp(9) : error C2676: binary '==' : 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' does not define this operator or a conversion to a
type acceptable to the predefined operator
Error executing cl.exe.

Visa signatur

Våga inte ens prata filosofi med mig...

Permalänk

ps: har du icq och e sugen på att hjälpa mig kan du ju adda mig på
151859063

Visa signatur

Våga inte ens prata filosofi med mig...

Permalänk
Medlem

"Have you in your possession a Compiler of Source Code?" the Master asked.
Vad är det för kompilator du har IamFenster?

Visa signatur

Scud: The other day, in the park, I was wondering why frisbees look bigger and bigger as they get closer to you
Scud: And then it hit me

Permalänk
Medlem
Citat:

Ursprungligen inskrivet av Gannon
"Have you in your possession a Compiler of Source Code?" the Master asked.
Vad är det för kompilator du har IamFenster?

cl.exe brukar vara Microsofts kompilator...

Visa signatur

"`F' is group reply. This is because `G' is for sort and `g' is for get new mail. And `G' is for sort because `sort' used be called `group', and anyway `S' is for save mailbox and `s' is for file message in folder, because `f' is group reply and `F' is group reply including original text."