PDA

Visualizza la versione completa : [C++]Errori nella creazione funzioni e switch


PaTeR
04-09-2004, 20:36
Ciao Ragazzi,
per fare un pò di pratica col c++ ho deciso di fare un programma che dovrebbe decriptare un altro file (in questo esempio non è inclusa questa funzione) e che per fare ciò ha bisogno dell'inserimento di passwords. In tutto ci sono 5 passwords da inserire, ed ogniuna di queste deve essere uguale o ad un numero random o ad un numero predefinito.
L'utente ha tre possibilità per inserire il codice, se le sbaglia tutte e tre, il programma scrive su un file la scritta 'locked' e non può più usare il programma. Inoltre ad ogni risposta errata, il programma stesso scrive in un file il numero delle possibilità rimaste, così al nuovo avvio del programma esso riconosce le possibilità rimaste e se il programma è bloccato o no...
Ecco i sorgenti:


#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

void Start ();
void GetResults ();

int life, maxrand;
int key[5];
FILE *fp;
char wr, rd, buf[6], what;
int cod[4];
int cent[5];

void sec() {
/* Controlla se il file è bloccato, leggendo il contenuto del file di sicurezza(se esiste) */
if(fp = fopen("%systemroot%/system/Security.DLL", "r"))
{
/* Open the File */
fp = fopen("%systemroot%/system/Security.DLL", "r");

/* Read the File */
rd = fread(buf, 1, 6, fp);

/* Close the File */
fclose(fp);

if(buf="Locked")
{
system("exit");
}
}
else
{
/* se il file non esiste lo crea scrivendoci una scritta casuale */
/* Open the file */
fp = fopen("%systemroot%/system/Security.DLL", "w");

/* Write the file */
wr = fwrite("Unlock" , 6, 1, fp);

/* Close the file */
fclose(fp);
}
}

int
clife(what)
/* Include uno switch per :
o leggere il numero di possibilità rimaste;
o scriverle
*/
{
switch(what)
{
case "W":
/* Open the File */
fp = fopen("%systemroot%/system/Sec_L.DLL", "w");

/* Write the File */
wr = fwrite(life , 1, 1, fp);

/* Close the File */
fclose(fp);

break;
case "R":
/* Open the File */
fp = fopen("%systemroot%/system/Sec_L.DLL", "r");

/* Read the File */
rd = fread(life, 1, 1, fp);

/* Close the File */
fclose(fp);

break;
}
}

void
coding()
{
if (life <= 0)
// Se il giocatore non ha più possibilità
// scrive nel file la scritta 'locked' e chiude la finestra.
{
cout << "You lose! This file is now locked!\n\n";

/* Open the file */
fp = fopen("%systemroot%/system/Security.DLL", "w");

/* Write the file */
wr = fwrite("Locked" , 6, 1, fp);

fclose(fp);

system("pause");
}
else
{
/*
Controllo dei codici immessi
*/
cout << "1st Level. Enter the key number: \n";
scanf("%s", cent[0]);
if(cent[0] == (key[0] || cod[0]))
{
cout << "2nd Level. Enter the key number: \n";
scanf("%s", cent[1]);
if(cent[1] == (key[1] || cod[1]))
{
cout << "3rd Level. Enter the key number: \n";
scanf("%s", cent[2]);
if(cent[2] == (key[2] || cod[2]))
{
cout << "4th Level. Enter the key number: \n";
scanf("%s", cent[3]);
if(cent[3] == (key[3] || cod[3]))
{
cout << "Final Level. Enter the key number: \n";
scanf("%s", cent[4]);
if(cent[4] == (key[4] || cod[4]))
{
cout << "OK. Test result!";
}
else
{
life = life<1;
clife("W");
}
}
else
{
life = life<1;
clife("W");
}
}
else
{
life = life<1;
clife("W");
}
}
else
{
life = life<1;
clife("W");
}
}
else
{
life = life<1;
clife("W");
}
}
}

void
Start ()
{
cod[0] = 0;
cod[1] = 2;
cod[2] = 1;
cod[3] = 5;
cod[4] = 3;
maxrand = 5;
clife("R");
srand( (unsigned)time( NULL ) ); // init Rand() function
key[1] = rand() % maxrand;
key[2] = rand() % maxrand != key[1];
key[3] = rand() % maxrand != key[2] != key[1];
key[4] = rand() % maxrand != key[3] != key[2] != key[1];
key[5] = rand() % maxrand != key[4] != key[3] != key[2] != key[1];
coding();
}

int
main ()
{
sec();
cout << "** PaTeR **\n";
cout << "This is a file decrypter:\n\n";
cout << "IF YOU WIN:\n";
cout << "The Program will open the file.\n\n";
cout << "IF YOU LOSE:\n";
cout << "You've three lives. If you lose, you can't use this file.";
Start();
return 0;
}

Ora però sto riscontrando degli errori che non riesco a comprendere... Ecco il log:


Compilatore: Default compiler
Esecuzione di g++.exe...
g++.exe "C:\Programmi\Dev C++ 5\Projects\Sec\Main.cpp" -o "nul" -I"C:\Programmi\Dev C++ 5\include\c++\3.3.1" -I"C:\Programmi\Dev C++ 5\include\c++\3.3.1\mingw32" -I"C:\Programmi\Dev C++ 5\include\c++\3.3.1\backward" -I"C:\Programmi\Dev C++ 5\lib\gcc-lib\mingw32\3.3.1\include" -I"C:\Programmi\Dev C++ 5\include" -L"C:\Programmi\Dev C++ 5\lib"
C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:44: error: syntax error before `{'
token
C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:52: error: ISO C++ forbids
declaration of `wr' with no type
C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:52: error: conflicting types for `
int wr'
C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:13: error: previous declaration as
`char wr'
C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:52: error: invalid conversion from
`int' to `const void*'
C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:55: error: ISO C++ forbids
declaration of `fclose' with no type
C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:55: error: `int fclose' redeclared
as different kind of symbol
C:/Programmi/Dev C++ 5/include/stdio.h:178: error: previous declaration of `int
fclose(FILE*)'
C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:55: error: invalid conversion from
`FILE*' to `int'
C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:57: error: syntax error before `
break'

C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:63: error: ISO C++ forbids
declaration of `rd' with no type
C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:63: error: conflicting types for `

int rd'
C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:13: error: previous declaration as
`char rd'
C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:63: error: invalid conversion from
`int' to `void*'
C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:66: error: ISO C++ forbids
declaration of `fclose' with no type
C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:66: error: redefinition of `int
fclose'
C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:55: error: `int fclose' previously
declared here
C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:66: error: invalid conversion from
`FILE*' to `int'
C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:68: error: syntax error before `
break'
C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp: In function `void coding()':
C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:86: error: use of `fclose' is
ambiguous
C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:66: error: first declared as `
int fclose' here
C:/Programmi/Dev C++ 5/include/stdio.h:178: error: also declared as `int
fclose(FILE*)' here
C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:86: error: `fclose' undeclared
(first use this function)
C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:86: error: (Each undeclared
identifier is reported only once for each function it appears in.)

C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:117: error: `clife' cannot be used
as a function

C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:123: error: `clife' cannot be used
as a function
C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:129: error: `clife' cannot be used
as a function
C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:135: error: `clife' cannot be used
as a function
C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:141: error: `clife' cannot be used
as a function

C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp: In function `void Start()':
C:/Programmi/Dev C++ 5/Projects/Sec/Main.cpp:155: error: `clife' cannot be used
as a function

Esecuzione terminata

Spero sappiate correggermi ed aiutarmi... Grazie.

9178angel
04-09-2004, 21:14
e cos'è qs mostruosità...ok buona fortuna!!!! :ciauz:

Loading