0

This program I made for request for my gf with the scraps I found on the internet. Its suppose to ask the questions (yes I know they are in polish, but it doesnt matter what they say) in the order given in the program (its important) and it works fine BUT only if you answer with a single word or letter... for example "Yes" or "Y" - if you answer like "Yes Yes" It will skip the second question and go straight to the third one. Basically the more words you use to answer a single question, the more questions the program will skip (fe. you answered "yes yes yes" in the first question, you can see that it will skip 3 questions and go to question number four, and then if you answer "yes" it will work correctly and will go to the next (fifth) question)

I hope its clear what I wrote, Im not familiar with c++ since I dont like that language still it has to be done so I get laid so, HELP GUYS.

Ps. the std::string PL( const char * text ) module is simply to get a polish letters if anyone wonders.

#include <iostream>

#include <windows.h>

#include <conio.h> 

#include <stdlib.h>


using namespace std;

std::string PL( const char * text )
{
    std::string result;
    for(; * text; text++ )
    switch( * text )
    {
    case 'ą': result += static_cast < char >( 0xa5 ); break;
    case 'ć': result += static_cast < char >( 0x86 ); break;
    case 'ę': result += static_cast < char >( 0xa9 ); break;
    case 'ł': result += static_cast < char >( 0x88 ); break;
    case 'ń': result += static_cast < char >( 0xe4 ); break;
    case 'ó': result += static_cast < char >( 0xa2 ); break;
    case 'ś': result += static_cast < char >( 0x98 ); break;
    case 'ż': result += static_cast < char >( 0xbe ); break;
    case 'ź': result += static_cast < char >( 0xab ); break;
    case 'Ą': result += static_cast < char >( 0xa4 ); break;
    case 'Ć': result += static_cast < char >( 0x8f ); break;
    case 'Ę': result += static_cast < char >( 0xa8 ); break;
    case 'Ł': result += static_cast < char >( 0x9d ); break;
    case 'Ń': result += static_cast < char >( 0xe3 ); break;
    case 'Ó': result += static_cast < char >( 0xe0 ); break;
    case 'Ś': result += static_cast < char >( 0x97 ); break;
    case 'Ż': result += static_cast < char >( 0xbd ); break;
    case 'Ź': result += static_cast < char >( 0x8d ); break;
        default: result += * text; break;
    }
    return result;
}


int main()
{
    SetConsoleTitle ("martwy zajac");


HWND hwnd = GetConsoleWindow();
if( hwnd != NULL ){ MoveWindow(hwnd ,340,100 ,800,300 ,TRUE); }


    string odpowiedz1,odpowiedz2,odpowiedz3,odpowiedz4,odpowiedz5,odpowiedz6,odpowiedz7,odpowiedz8,odpowiedz9,odpowiedz10,odpowiedz11,odpowiedz12,odpowiedz13,odpowiedz14,odpowiedz15,odpowiedz16,odpowiedz17,odpowiedz18,odpowiedz19,odpowiedz20,odpowiedz21;



    cout <<PL("martwy zając:\n\n");
    cout <<PL("Witaj, chciałbym żebyś wytłumaczył mi dzieło sztuki.\n\n");
    Sleep(3000);
    cout <<PL("Wciśnij ENTER jeśli jesteś gotowy.");
    _getch();

    system("cls");
    cout <<PL("martwy zając:\n\n");
    cout <<PL("Do czego sluży żaba, a do czego szutka?\n\n");
    cin >>odpowiedz1;
    system("cls");


    cout <<PL("martwy zając:\n\n");
    cout <<PL("Która łapa tego kota jest ładniejsza?\n\n");
    cin >>odpowiedz2;
    system("cls");

    cout <<PL("martwy zając:\n\n");
    cout <<PL("Czy ta łapa, która bardziej się Tobie podoba jest ładniejsza gdy kot miauczy\nprosząc o jedzenie, czy jak miauczy prosząc o pieszczotę?\n\n");
    cin >>odpowiedz3;
    system("cls");

    cout <<PL("martwy zając:\n\n");
    cout <<PL("Czy wolisz dzwięk tramwaju dobiegający zza okna w mroźny poranek, czy w jesiennywieczór?\n\n");
    cin >> odpowiedz4;
    system("cls");

    cout <<PL("martwy zając:\n\n");
    cout <<PL("Czy możesz opisać smak ciastka które jadłeś ostatnio?\n\n");
    cin >> odpowiedz5;
    system("cls");

    cout <<PL("martwy zając:\n\n");
    cout <<PL("Namalujesz mi ten smak, jaki ma kolor?\n\n");
    cin >> odpowiedz6;
    system("cls");

    cout <<PL("martwy zając:\n\n");
    cout <<PL("Na kafelki w łazience upadła mi gąbka - jakiego koloru jest dzwięk który wydała?\n\n");
    cin >> odpowiedz7;
    system("cls");

    cout <<PL("martwy zając:\n\n");
    cout <<PL("Na kafelki w łazience upadła mi szklanka, nie rozbiła się - jakiego koloru dzwięk wydała?\n\n");
    cin >> odpowiedz8;
    system("cls");

    cout <<PL("martwy zając:\n\n");
    cout <<PL("Cyfra 4 jest niebieska czy szara?\n\n");
    cin >> odpowiedz9;
    system("cls");

    cout <<PL("martwy zając:\n\n");
    cout <<PL("Wchodzę. Czy widzę światło?\n\n");
    cin >> odpowiedz10;
    system("cls");

    cout <<PL("martwy zając:\n\n");
    cout <<PL("Wchodzę. Czy słyszę światło?\n\n");
    cin >> odpowiedz11;
    system("cls");

    cout <<PL("martwy zając:\n\n");
    cout <<PL("Wchodzę. Czy czuję smak światła?\n\n");
    cin >> odpowiedz12;
    system("cls");

    cout <<PL("martwy zając:\n\n");
    cout <<PL("Cyfra 4 czy zmienia kolor, gdy staje się liczbą?\n\n");
    cin >> odpowiedz13;
    system("cls");

    cout <<PL("martwy zając:\n\n");
    cout <<PL("Biegnę. Co się zmieniło gdy biegnę a nie wchodzę? Widok światła? Dzwięk światła?Smak światła?\n\n");
    cin >> odpowiedz14;
    system("cls");

    cout <<PL("martwy zając:\n\n");
    cout <<PL("Gdy pytałam o kolor smaku ciastka, jakie pytanie usłyszałeś/aś?\n\n");
    cin >> odpowiedz15;
    system("cls");

    cout <<PL("martwy zając:\n\n");
    cout <<PL("Jaki kolor ma słowo \"sztuka?\"\n\n");
    cin >> odpowiedz16;
    system("cls");

    cout <<PL("martwy zając:\n\n");
    cout <<PL("Jaki kolor ma słowo \"religia?\"\n\n");
    cin >> odpowiedz17;
    system("cls");

    cout <<PL("martwy zając:\n\n");
    cout <<PL("Jaki kolor ma słowo \"jestem?\"\n\n");
    cin >> odpowiedz18;
    system("cls");

    cout <<PL("martwy zając:\n\n");
    cout <<PL("Godzina 16:30 jest sześcianem, kulą, czy inną bryłą?\n\n");
    cin >> odpowiedz19;
    system("cls");

    cout <<PL("martwy zając:\n\n");
    cout <<PL("Godzina 4:30 (po południu) jest sześcianem, kulą, czy inną bryłą?\n\n");
    cin >> odpowiedz20;
    system("cls");

    cout <<PL("martwy zając:\n\n");
    cout <<PL("Jak przekroczyć zmysłowy pozór sztuki?\n\n");
    cin >> odpowiedz21;
    system("cls");

    cout <<PL("martwy zając:\n\n");
    cout <<PL("Dziękuję\n\n\n");
    Sleep(3000); 
    cout <<PL("Żegnaj.");
    Sleep(2000);


}
1
  • What you need is std::ignore.
    – DimChtz
    Aug 27, 2016 at 21:57

2 Answers 2

1

According to the documentation of the extraction operator (<<) of cin,

[...] it always considers spaces (whitespaces, tabs, new-line...) as terminating the value being extracted, and thus extracting a string means to always extract a single word, not a phrase or an entire sentence.

(See http://www.cplusplus.com/doc/tutorial/basic_io/ section "cin and strings")

So, what you are observing is exactly in line with the documented behavior: if you type "yes1 yes2" and hit enter, the cin << my_string_variable instruction will store "yes1" in my_string_variable and it will keep "yes2" for the next cin.

To overcome this problem, use the following:

getline (cin, my_string_variable);
1
  • 1
    IT WORKS! Thank you very much Mike !! Have a good day :) edit: oh you are from greece, then sleep tight ;))
    – Wojciech
    Aug 27, 2016 at 22:06
0

What you need is getting a whole line of input from cin and only processing the first char. Try the following code:

  #include <iostream>
  using namespace std;
  int main(){
    char a[1000],c;
    cin.getline(a,1000);
    cout<<"\nThe string received:"<<endl<<a<<endl;
    cout<<"\nThe answer to process:"<<endl<<a[0]<<endl;
    return 0;
  }

The key command is getline. You may need to write a loop to find the first non-space character and turn upper case into lower case to regularize the answer. If it's neither 'y' or 'n' your code should say "Sorry, I don't understand your answer. Please say yes (y) or no (n)" and ask for the answer again in a while loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.