Kun toi lemmikki kysymyksen vastaus ei toimi?
#include
int main(void)
{
char nimi[50];
char koti[50];
char pet[3];
char loppu[128];
printf("*****Hello -keskustelu v0.7***** \n***Created by:Mika Katajamaki*** \n*********2.7.2006 23:42*********\n");
printf("\nHello! Who are you?\n"); //Kysyy
gets_s(nimi); // annetaan vastaus
printf("\nHow are you %s?\n",nimi);
printf("\nWhere do you live?\n");
gets_s(koti);
printf("\n%s must be intresting place to live.\n",koti);
printf("\nOk, Do you have any pets? yes or no?\n"); //EI TOIMI
gets_s(pet);
if (pet == yes) printf("\nI have 2 Hard-drives as a pet. Aren't they so cute?!\n");
if (pet == no) printf("\nOh, that's kind of a sad.\n");
printf("\nI have reboot now. See ya later! \nPlease press ENTER to exit\n");
gets_s(loppu);
printf("\n%s BYE!\n",loppu);
}
#include
int main(void)
{
char nimi[50];
char koti[50];
char pet[3];
char loppu[128];
printf("*****Hello -keskustelu v0.7***** \n***Created by:Mika Katajamaki*** \n*********2.7.2006 23:42*********\n");
printf("\nHello! Who are you?\n"); //Kysyy
gets_s(nimi); // annetaan vastaus
printf("\nHow are you %s?\n",nimi);
printf("\nWhere do you live?\n");
gets_s(koti);
printf("\n%s must be intresting place to live.\n",koti);
printf("\nOk, Do you have any pets? yes or no?\n"); //EI TOIMI
gets_s(pet);
if (pet == yes) printf("\nI have 2 Hard-drives as a pet. Aren't they so cute?!\n");
if (pet == no) printf("\nOh, that's kind of a sad.\n");
printf("\nI have reboot now. See ya later! \nPlease press ENTER to exit\n");
gets_s(loppu);
printf("\n%s BYE!\n",loppu);
}
Vastaukset 16
Vanhimmat
Tos on .exe jossa ei oo sit lemmikki kyssärii
http://www.freewebs.com/lahha/Hello.exe
printf("\nOk, Do you have any pets? yes or no?\n"); //EI TOIMI
->
printf("\nOk, Do you have any pets? yes or no?\n",pet);
Ei taida tuokaan oikein toimia, sillä printf:n ensimmäiseen argumenttiin pitää lisätä %s sopivaan kohtaan, jos siihen kohtaan halutaan lisätä tuon pet-muuttujan sisältämä sana. Jos sitä ei haluta tulostaa (ilmeisesti kuitenkin tuon tulostuksen jälkeen halutaankin vain lukea se), niin sitten ", pet" pitää poistaa.
Merkkijonojen vertailu ei mene C:ssä noin suoraan ==:lla, vaan siihen pitää käyttää erillisiä funktioita, kuten strcmp tai strncmp.
include
if (strcmp("yes", pet) == 0)
printf("on\n");
else if (strcmp("no", pet) == 0)
printf("ei ole\n");
else
printf("vastasit jotain tuubaa\n");
mikä neuvoks?
mut miten sen saa sillai et jos vastaa jotain muuta kuin "yes" tai "no" nii et kiertää niin kau uuestaa kunnes vastaa "yes" tai "no". ku ny se menee vaa siitä kohast eteenpäi
Yksi vika löytyy pet-muuttujan esittelystä. Kolmen merkin mittainen taulukko ei riitä vastaukselle 'yes'.
ny ois enää tää ongelma:
http://keskustelu.suomi24.fi/show.fcgi?category=108&conference=4500000000000140&posting=22000000017590537
int main(void)
{
char nimi[50];
char vointi[50];
char koti[50];
char pet[5];
char väri[10];
char loppu[128];
printf("*****Hello -keskustelu v0.9***** \n***Created by:Mika Katajamaki*** \n*********2.7.2006 23:42*********\n");
//###jutustelu alkaa###
printf("\n-Hello! Who are you?\n"); //Kysyy
gets_s(nimi); // annetaan vastaus
printf("\n-How are you %s?\n",nimi);
gets_s(vointi);
printf("\n-I'm %s too, thank you.\n",vointi); //kommentoi vastausta
printf("\n-Where do you live?\n");
gets_s(koti);
printf("\n-%s must be intresting place to live.\n",koti);
printf("\n-What is your favourite color?\n",nimi);
gets_s(väri);
printf("\n-%s is my fav color too.\n",väri);
printf("\n-Do you have any pets? Yes or no?\n");
gets_s(pet);
if (strcmp("yes", pet) == 0)
printf("\n-Really! I have two hard-drives as pet.\n");
else if (strcmp("no", pet) == 0)
printf("\n-How sad!\n");
else
printf("-Yes or no?\n");
printf("\n-I have to reboot now. See ya later! \nPlease press ENTER to exit\n");
gets_s(loppu);
printf("\n%s BYE!\n",loppu);
}
miten sen saa sillai et jos vastaa jotain muuta kuin "yes" tai "no" nii et kiertää niin kau uuestaa kunnes vastaa "yes" tai "no" ja sit vast menee eteenpäi.
Esim. jotain vastaavaa:
int vastaus_on_ok = 0;
....
while (vastaus_on_ok == 0) {
if (strcmp("yes", pet) == 0) {
printf("\n-Really! I have two hard-drives as pet.\n");
vastaus_on_ok = 1;
} else if (strcmp("no", pet) == 0) {
printf("\n-How sad!\n");
vastaus_on_ok = 1;
} else
printf("-Yes or no?\n");
}
mut ny sit se ei jatka eteenpäin kun kysymykseen on saatu oike vastaus.
ja täs taas nykyinen koodi(hiukan selvennetty)
#include //En tiiä tarvitaanko mut softa pakottaa pistää.
#include
#include
#include
#include
int main(void)
{
char nimi[50];
char vointi[50];
char koti[50];
char pet[5];
char kys[5];
char vari[10];
char loppu[128];
int vastaus_on_ok = 0;
printf("*****Hello -keskustelu v0.9***** \n***Created by:Mika Katajamaki*** \n*********2.7.2006 23:42*********\n*******(c)Mika Katajamaki*******\n");
//###jutustelu alkaa###
printf("\n-Hello! Who are you?\n"); //Kysyy
gets_s(nimi); // annetaan vastaus
printf("\n-How are you %s?\n",nimi);
gets_s(vointi);
printf("\n-I'm %s too, thank you.\n",vointi); //kommentoi vastausta
//################################################################
printf("\n-Where do you live?\n");
gets_s(koti);
printf("\n-%s must be intresting place to live.\n",koti);
//################################################################
printf("\n-Do you have any pets? Yes or no?\n");
gets_s(pet);
while (vastaus_on_ok == 0) {
if (strcmp("yes", pet) == 0) {
printf("\n-Really! I have two hard-drives as pet.\n");
vastaus_on_ok = 1;
} else if (strcmp("no", pet) == 0) {
printf("\n-How sad!\n");
vastaus_on_ok = 1;
} else
printf("-Yes or no?\n");
gets_s(pet);
}
//################################################################
printf("\n-What is your favourite color?\n",nimi);
gets_s(vari);
printf("\n-%s is my fav color too.\n",vari);
//#################################################################
printf("\n-Do you like computers?\n");
gets_s(kys);
while (vastaus_on_ok == 0) {
if (strcmp("yes", kys) == 0) {
printf("\n-Really! I have two hard-drives as pet.\n");
vastaus_on_ok = 1;
} else if (strcmp("no", kys) == 0) {
printf("\n-How sad!\n");
vastaus_on_ok = 1;
} else
printf("-Yes or no?\n");
gets_s(kys);
}
//################################################################
printf("\n-I have to reboot now. See ya later! \nPlease press ENTER to exit\n");
gets_s(loppu);
printf("\n BYE!\n");
//##########################THE END##############################
se menee eteenpäin kun painaa enteriä.
Viel on toi edellisen viestin ongelma.
täs ois taas uusinta versioo
http://www.freewebs.com/lahha/Hello.zip
printf("\n-Do you have any pets? Yes or no?\n");
while (1) {
gets_s(pet);
if (strcmp("yes", pet) == 0)
{
printf("\n-Really! I have two hard-drives as a pet.\n");
break;
}
else
if (strcmp("no", pet) == 0)
{
printf("\n-How sad!\n");
break;
}
else
printf("-Yes or no?\n");
}
Nyt ohjelma toimii niinkuin pitäisi!
Uusin versio löytyy:
http://www.freewebs.com/lahha