c++ Błędy z klasą String

0

#include <iostream>
#include <string>
#include <conio.h>
#include <cstdlib>
using namespace std;

int main()

{
int menu;

N: cin.clear();
cin.sync();
cout<<"\n[3] Zaszyfruj na Szyfr Cezara";
cout<<"\n\n[4] deszyfruj";
cout<<"\n\nWybieram: "; cin>>menu;
if(menu==4) goto O;
if(menu==3) goto F;
else goto N;

F: cin.clear();
cin.sync();

string tekst;

cout<<"\n_______________________________________\n\n";
cout<<"Podaj tekst do zaszyfrowania: ";
cin >>tekst;

for(int i=0;i<=tekst.length();i++){
if(tekst[i]>=65 && tekst[i]<=90-3) tekst[i]=int(tekst[i])+3;
else if(tekst[i]>=91-3 && tekst[i]<=90) tekst[i]=int(tekst[i])-26+3;
else if(tekst[i]>=97 && tekst[i]<=122-3) tekst[i]=int(tekst[i])+3;
else if(tekst[i]>=123-3 && tekst[i]<=122) tekst[i]=int(tekst[i])-26+3;
}
cout<<"\n\nZaszyfrowany tekst: "<<tekst<<endl;
goto N;

//-------------------------------------------------------------

O:
cin.clear();
cin.sync();

string ttkst;

cout<<"\n_______________________________________\n\n";
cout<<"Podaj tekst do deszyfrowania: ";
cin >>ttkst;

for(int i=0;i<=ttkst.length();i++){
if(ttkst[i]>=65 && ttkst[i]<=90+3) ttkst[i]=int(ttkst[i])-3;
else if(ttkst[i]>=91+3 && ttkst[i]<=90) ttkst[i]=int(ttkst[i])-26-3;
else if(ttkst[i]>=97 && ttkst[i]<=122+3) ttkst[i]=int(ttkst[i])-3;
else if(ttkst[i]>=123+3 && ttkst[i]<=122) ttkst[i]=int(ttkst[i])-26-3;
}
cout<<"\n\nZaszyfrowany tekst: "<<ttkst<<endl;
goto N;

}

Nie działa.. Czemu?
Program ma szyfrować kodem Cezara i odszyfrowywać.

1

Strach się bać, pogrom widzę, pogrom w biały dzień.

Ten kod, nawet jak dla mnie, to jeden wielki WTF, a zwłaszcza używanie goto w pozostałych przypadkach niż wyjście z baaardzo zagnieżdżonej pętli.

Masz tu ode mnie wersję bardziej ludzką:

#include <iostream>
#include <string>

using namespace std ;

string encryptText(string text)
{
  size_t length = text.length() ;

  for (size_t i = 0 ; i < length ; i++)
  {
    if (text[i] >= 'A' && text[i] <= 'Z')
    {
        text[i] += 3 ;
        if (text[i] > 'Z') text[i] -= 26;
    }
    else if (text[i] >= 'a' && text[i] <= 'z')
    {
        text[i] += 3 ;
        if (text[i] > 'z') text[i] -= 26;
    }
    else if (text[i] >= '0' && text[i] <= '9')
    {
        text[i] += 3 ;
        if (text[i] > '9') text[i] -= 10;
    }
  }

  return text ;
}

string decryptText(string text)
{
  size_t length = text.length() ;

  for (size_t i = 0 ; i < length ; i++)
  {
    if (text[i] >= 'A' && text[i] <= 'Z')
    {
        text[i] -= 3 ;
        if (text[i] > 'Z') text[i] += 26;
    }
    else if (text[i] >= 'a' && text[i] <= 'z')
    {
        text[i] -= 3 ;
        if (text[i] > 'z') text[i] += 26;
    }
    else if (text[i] >= '0' && text[i] <= '9')
    {
        text[i] -= 3 ;
        if (text[i] > '9') text[i] += 10;
    }
  }

  return text ;
}

int main()
{
  string text ;
  int choice ;

  cout << "Wybierz dzialanie:" << endl ;
  cout << "1) - Szyfrowanie" << endl ;
  cout << "2) - Deszyfrowanie" << endl ;

  cin >> choice ;

  cin.ignore() ;

  switch(choice)
  {
    case 1:
      cout << "Podaj tekst: " ;
      getline(cin, text) ;

      cout << "Zaszyfrowany tekst:" << endl ;
      cout << encryptText(text) << endl ;

      break ;

    case 2:
      cout << "Podaj tekst: " ;
      getline(cin, text) ;

      cout << "Rozszyfrowany tekst:" << endl ;
      cout << decryptText(text) << endl ;

      break ;

    default:
      cout << "Nieprawidlowy wybor" << endl ;
      break ;
  }
}
2

Może jeszcze trochę skoryguję kod od @Lord Darkstorm:

string encryptCesar(string text,unsigned key)
  {
   key%=26;
   for(size_t i=0;i<text.length();++i)
     {
      if(isupper(text[i])) text[i]='A'+(text[i]-'A'+key)%26;
      else if(islower(text[i])) text[i]='a'+(text[i]-'a'+key)%26;
      else if(isdigit(text[i])) text[i]='0'+(text[i]-'0'+key)%10;
     }
   return text;
  }

 string decryptCesar(string text,unsigned key)
  {
   return encryptCesar(text,26-key%26);
  }

1 użytkowników online, w tym zalogowanych: 0, gości: 1