1

I have a wird problem. Im using Visual Studio 2012, thats my code: (something is in polish but i hope you will understand how it works).

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <queue>

using namespace std;

#pragma warning (disable: 4996);

struct strona
{
    int zawartosc;
    int gdzie;

};

void Wyswietl(int tab[], int dlugosc)
{
    cout<<"Tablica wyglada tak"<<endl;
    for(int i=0;i<dlugosc;i++)
    {
        cout<<tab[i]<<"\t";
    }
    cout<<endl;
}


int main()
{
    int ileStron=3;


    int *tablicaStron, *tablicaBitowOdniesienia;
    tablicaStron=new int[ileStron];
    tablicaBitowOdniesienia=new int[ileStron];


    queue <strona> kolejka;


    char opcja='a';
    while(opcja!='k')
    {
        cout<<"(D)odawac (K)oniec (W)yswietl";
        cin>>opcja;//DONT STOP THE PROGRAM!
        if(opcja=='D'|opcja=='d')
        {
            strona tymczas;
            cout<<"Podaj co dodać do kolejki";
            cin>>tymczas.zawartosc;
            int licznik=0;
            if(kolejka.size()<ileStron)
            {
                tymczas.gdzie=kolejka.size();

                kolejka.push(tymczas);
                tablicaStron[tymczas.gdzie]=tymczas.zawartosc;
            }
            else if(kolejka.size()==ileStron)
            {
                cout<<"bang bang";
                int czyJest=0;
                int licznikfora=0;
                for(int i=0;i<ileStron;i++)//sprawdza czy wpisywana strona nie istnieje przypadkiem w tablicy stron
                {
                    if(tablicaStron[i]==tymczas.zawartosc)
                    {
                        czyJest=1;

                    }

                    licznikfora++;
                }
                cout<<"czyJest ma wartosc "<<czyJest<<" a licznik fora "<<licznikfora<<endl;
                if(czyJest==0)
                {
                    tymczas.gdzie=kolejka.front().gdzie;
                    kolejka.pop();//TUTAJ SIE BEDZIE ZAPISYWAC DO PAMIECI WIRTUALNEJ
                    kolejka.push(tymczas);
                    tablicaStron[tymczas.gdzie]=tymczas.zawartosc;
                }
                else if(czyJest==1)
                {
                    cout<<"to co chcesz dodac juz jest w pamieci";
                }
            }
            else 
            {
                cout<<"rozmiar kolejki sie nie zgadza";
            }

        }
        else if(opcja=='W'|opcja=='w')
        {
            Wyswietl(tablicaStron,ileStron);
            cout<<endl;
            cout<<"pierwszy element w kol: "<<kolejka.front().zawartosc<<"|"<<kolejka.front().gdzie<<"  "
                <<"ostatni element w kol: "<<kolejka.back().zawartosc<<"|"<<kolejka.back().gdzie<<endl;
        }
    }






    system("pause");
    return 0;

}

The problem is that after choosing option (d) - just type d and press enter, then type any few letters and the program should show you

(D)odawac (K)oniec (W)yswietl

but it is starting to loop to infinity...

What is the problem?

wiwo
  • 721
  • 1
  • 13
  • 18
  • did you try scanf? When you say infinite loop, is it printing "(D)odawac (K)oniec (W)yswietl" infinitely? – rplusg Jan 11 '14 at 05:10
  • it shows (D)odawac (K)oniec (W)yswietl: d Podaj co dodaŠ do kolejki: q and this inifinitely: (D)odawac (K)oniec (W)yswietl: Podaj co dodaŠ do kolejki: (D)odawac (K)oniec (W yswietl: Podaj co dodaŠ do kolejki: (D)odawac (K)oniec (W)yswietl: Podaj co dod Š do kolejki: bang bangczyJest ma wartosc 1 a licznik fora 3 to co chcesz dodac juz jest w pamieci(D)odawac (K)oniec (W)yswietl: – wiwo Jan 11 '14 at 05:26
  • and i tried scanf - the same! – wiwo Jan 11 '14 at 05:29

1 Answers1

0

I think problem is with this statement: cin>>tymczas.zawartosc;

At this point if you give a char or string input, program blows up.

to make your program work properly: start the program and give only one input d, let the control hit this point : cin>>tymczas.zawartosc; now input a number here, as it is int type. now control hits first cin.

The problem is with using cin to read int but user inputs a char or string. Behaviour is undefined in this case. This link should resolve confusion: C++ character to int

Community
  • 1
  • 1
rplusg
  • 3,418
  • 6
  • 34
  • 49
  • im an idiot... thanks a lot for help! Its a small part of quite a big project that i have to send till 8am (its 6:41am and i didnt sleep)! – wiwo Jan 11 '14 at 05:42
  • Sorry man, and I just woke up, let me know if you want anymore details. Happy to help :) – rplusg Jan 11 '14 at 05:45