Skocz do zawartości
  • 👋 Witaj na MPCForum!

    Przeglądasz forum jako gość, co oznacza, że wiele świetnych funkcji jest jeszcze przed Tobą! 😎

    • Pełny dostęp do działów i ukrytych treści
    • Możliwość pisania i odpowiadania w tematach
    • System prywatnych wiadomości
    • Zbieranie reputacji i rozwijanie swojego profilu
    • Członkostwo w jednej z największych społeczności graczy

    👉 Dołączenie zajmie Ci mniej niż minutę – a zyskasz znacznie więcej!

    Zarejestruj się teraz

Błąd programu w c++


Rekomendowane odpowiedzi

Opublikowano
#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>=65 && tekst<=90-3) tekst=int(tekst)+3;

else if(tekst>=91-3 && tekst<=90) tekst=int(tekst)-26+3;

else if(tekst>=97 && tekst<=122-3) tekst=int(tekst)+3;

else if(tekst>=123-3 && tekst<=122) tekst=int(tekst)-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>=65 && ttkst<=90+3) ttkst=int(ttkst)-3;

else if(ttkst>=91+3 && ttkst<=90) ttkst=int(ttkst)-26-3;

else if(ttkst>=97 && ttkst<=122+3) ttkst=int(ttkst)-3;

else if(ttkst>=123+3 && ttkst<=122) ttkst=int(ttkst)-26-3;

}

cout<<"\n\nZaszyfrowany tekst: "<<ttkst<<endl;

goto N;

 

 

}

 

 

Nie działa.. Czemu?

Program ma szyfrować kodem Cezara i odszyfrowywać.

Opublikowano

Co Nie działa? Goto to bardzo brzydki nawyk, używaj pętli i funkcji

3587513.png


Potrzebujesz pomocy? Pisz śmiało na PW/Skype/Gadu-Gadu!


(albo napisz na forum... ^.^)

Opublikowano

Daj kod w code, jeśli chodzi o goto, to wróć się do asemblera. Użyj pętli while.


Pomagam w projektach dotyczących programowania (C++/C/Java/C#/inne). Jak masz jakiś problem, napisz do mnie, wspólnie poszukamy rozwiązania ;).

Opublikowano

Deszyfrowanie tez musisz poprawic


@ Taka prostsza wersja (wraz z poprawnym deszyfrowaniem)

//szyfrowanie  
int x=3;     
        tekst[i]+=x;
	if(tekst[i]>90 && tekst[i]<90+x) tekst[i]-=26;
	else	if(tekst[i]>122) tekst[i]-=26;
	
//deszyfrowanie
	tekst[i]-=x;
	if(tekst[i]<90+x && tekst[i]>90) tekst[i]+=26;
	else	if(tekst[i]<65) tekst[i]+=26;
	
Opublikowano

goto i conio.h, ja bym się wstydził.

 

W ten sposób poprawiłem:

 

 

 

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

string ftext();
string decrypt(string);
string encrypt(string);
void show(string);
void menu();

bool bDupa;

/* ============================================================================ */

int main()
{
	for(; {
		menu();
	}
	return 0;
}

/* ============================================================================ */

void menu() 
{
	string sText;
	cout << "\nSzyfr Cezara\n\t1. Zaszyfruj\n\t2. Deszyfruj\n\t3. Wyjdz\nWybieram: ";
	cin.clear();
	short int snSwitch;
	cin >> snSwitch;
	switch(snSwitch) {
		case 1:
			sText = ftext();
			sText = encrypt(sText);
			bDupa = 1;
			show(sText);
			break;
		case 2:
			sText = ftext();
			sText = decrypt(sText);
			bDupa = 0;
			show(sText);
			break;
		case 3:
			exit(0);
			break;
		default:
			cout << "Cos poszlo nie tak...\n";
			menu();
			break;
	}
}

/* ============================================================================ */

string encrypt(string sText)
{
	for(int i = 0; i <= sText.length(); i++) {
		if(sText[i] >= 65 && sText[i] <= 90 - 3) sText[i] = int(sText[i]) + 3;
		else if(sText[i] >= 91 - 3 && sText[i] <= 90) sText[i] = int(sText[i]) - 26 + 3;
		else if(sText[i] >= 97 && sText[i] <= 122 - 3) sText[i] = int(sText[i]) + 3;
		else if(sText[i] >= 123 - 3 && sText[i] <= 122) sText[i] = int(sText[i]) - 26 + 3;
	}
	return sText;
}

/* ============================================================================ */

string decrypt(string sText)
{
	for(int i = 0; i <= sText.length(); i++) {
		if(sText[i] >= 65 && sText[i] <= 90 + 3) sText[i] = int(sText[i]) - 3;
		else if(sText[i] >= 91 + 3 && sText[i] <= 90) sText[i] = int(sText[i]) - 26 - 3;
		else if(sText[i] >= 97 && sText[i] <= 122 + 3) sText[i] = int(sText[i]) - 3;
		else if(sText[i] >= 123 + 3 && sText[i] <= 122) sText[i] = int(sText[i]) - 26 - 3;
	}
	return sText;
}

/* ============================================================================ */

string ftext()
{
	cout << "Podaj tekst: ";
	cin.clear();
	string sText;
	cin >> sText;
	return sText;
}

/* ============================================================================ */

void show(string sText)
{
	system("CLS");
	if(bDupa) {
		cout << "ZASZYFROWANY TEKST: " << sText << "\n";
	}
	else {
		cout << "ZDESZYFROWANY TEKST: " << sText << "\n";
	}
}

/* ============================================================================ */

 

 

 

Wybaczcie mi system("CLS");. :(

ㅈㅈ.

Opublikowano
#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>=65 && tekst<=90-3) tekst=int(tekst)+3;

else if(tekst>=91-3 && tekst<=90) tekst=int(tekst)-26+3;

else if(tekst>=97 && tekst<=122-3) tekst=int(tekst)+3;

else if(tekst>=123-3 && tekst<=122) tekst=int(tekst)-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<=tekst.length();i++){

if(ttkst>=65 && ttkst<=90+3) ttkst=int(ttkst)-3;

else if(ttkst>=91+3 && ttkst<=90) ttkst=int(ttkst)-26-3;

else if(ttkst>=97 && ttkst<=122+3) ttkst=int(ttkst)-3;

else if(ttkst>=123+3 && ttkst<=122) ttkst=int(ttkst)-26-3;

}

cout<<"\n\nZaszyfrowany tekst: "<<ttkst<<endl;

goto N;

 

 

}

 

Błąd w nazewnictwie funkcji :)

Zarchiwizowany

Ten temat przebywa obecnie w archiwum. Dodawanie nowych odpowiedzi zostało zablokowane.

×
×
  • Dodaj nową pozycję...