简体   繁体   中英

Program going into an infinite loop

That's my first question here, so I would be glad to receive some support on the style I used to refer to my problem :). Here is the finished program, its main purpose is to split given words into halves and create words replacing the origin ones. Replaced words are build from its origins by spliting them into halves and taking even ones from the 1st half begining with the first letter of a word. Heres the complete code:

#include <iostream>
#include <string>
#include <cstdio>
#include <math.h>

using namespace std;

void obcinaczSlow(int);

int main(){
    int ilosc;
    cout << "Prosze o podanie ilosci prob: ";
    cin>>ilosc;
    cout << endl;
    obcinaczSlow(ilosc);
    cin.ignore();
    cin.get();
    return 0;
}

void obcinaczSlow(int ilosc_prob){

    int i=0,j=0,dlugosc_slowa=0,dlugosc_polowy=0;
    string *tablica_slow,budowane_slowo,aktualne_slowo,dodane;
    tablica_slow = new string [ilosc_prob];

    cout << "Prosze o podanie " << ilosc_prob << " slow" << endl;
    cin.sync();
    for(i=0;i<ilosc_prob;i++){
        cout << "Prosze o podanie slowa numer: " << i+1 << endl;
        cin>>aktualne_slowo;
        tablica_slow[i] = aktualne_slowo;
    }

    for(i=0;i<ilosc_prob;i++){
        aktualne_slowo = tablica_slow[i];
        cout << "Aktualne slowo do przerobienia: " << aktualne_slowo << endl;
        dlugosc_slowa = aktualne_slowo.length();
        cout << "Dlugosc slowa do przerobienia: " << dlugosc_slowa << endl;
        dlugosc_polowy = floor(dlugosc_slowa/2);
        cout << "Dlugosc polowy slowa int: " << dlugosc_polowy << endl;
        budowane_slowo.clear();
        dodane.clear();
        cout << "Budowane slowo to: " << budowane_slowo << endl;
        for(j=0;j<=dlugosc_polowy;j=+2){
        dodane = aktualne_slowo.at(j);
        budowane_slowo.append(dodane);
        }
        tablica_slow[i] = budowane_slowo;
    }

    cout << "Slowa po transformacji wygladaja nastepujaco: " << endl;
    for(i=0;i<ilosc_prob;i++){
    cout << "Slowo o numerze " << i+1 << " : " << tablica_slow[i]  << endl;
    }
    delete [] tablica_slow;
    cin.sync();
}

The problem raises when program reaches the loop, that is supposed to append the letter pointed by the j-index using '.at' method from the string class. I can't find a solution even trying to debug it. Could You help me :)?

You have a typo here

for(j=0;j<=dlugosc_polowy;j=+2)

I assume you meant += instead of =+

for(j=0;j<=dlugosc_polowy;j+=2)

Otherwise you are just assigning 2 to j over and over again.

Your error is reversing two characters:
Change:

 `j=+2` to `j+=2`
   ^^        ^^   

(The way it is written j is assigned the value of 2, then, for the rest of its life, stays there.)

for(j=0;j<=dlugosc_polowy;j=+2){
    dodane = aktualne_slowo.at(j);
    budowane_slowo.append(dodane);
}

replace the j=+2 to j+=2

for(j=0;j<=dlugosc_polowy;j+=2){
    dodane = aktualne_slowo.at(j);
    budowane_slowo.append(dodane);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM