Hello everybody!
I'm new to this forum and first thing first I'd like to apologize for my terrible english, I hope you'll be able to understand me :)
Well...I started studying C++ this year (3 months ago), and right now I can write simple console apps that can record inputs, elaborate them and return an output, nothing awesome nor interesting. Anyway, I like this forum and I've been reading user's post or references to better understand what I'm trying to learn, and I realized that you guys usually advise people against doing some "mistakes"...
I mean, I read that using system() isn't a good thing since the code is OS-specific, that is a bad idea to use cin >> and cout << and I should use printf() and scanf(), etc.
So I was wondering, if I upload one of my codes, would you mind to read it and tell me where I can improve it? I know I should ask to my teacher to do that, but actually he's a 70yo incapable person and the only thing he does is drawing flowcharts.. I'm studying on my own so please don't be too harsh with me :)
This is the code, it works fine but I am quite sure it can be improved and that I'm missing many important thing..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int main()
{
system("color 9F");
float MaxNumeri=0;
float NumPosTot=0;
float Numero=0;
float QuantitaNumeri = 0;
float NumNegTot=0;
float PercentualePos=0;
float PercentualeNeg=0;
cout << endl;
cout << "Benvenuto, questo software permette di controllare una serie di numeri per determinare" << endl;
cout << " se sono positivi o negativi, la loro somma e la percentuale relativa." << endl;
cout << endl;
cout << "Quanti numeri desideri controllare? ";
cin >> MaxNumeri;
cout << endl;
cout << "Molto bene.";
cout << endl;
while (QuantitaNumeri<MaxNumeri)
{
cout << endl;
cout << "Inserisci un numero: ";
cin >> Numero;
QuantitaNumeri=QuantitaNumeri+1;
if (Numero<0)
{
NumNegTot=NumNegTot+1;
cout << "Hai inserito un numero negativo." << endl;
}
else if (Numero>0)
{
NumPosTot=NumPosTot+1;
cout << "Hai inserito un numero positivo." << endl;
}
else
{
cout << endl;
cout << "Non hai inserito un numero valido." << endl;
cout << endl;
system("pause");
return 0;
}
}
cout << endl;
PercentualePos=(NumPosTot/MaxNumeri)*100;
PercentualeNeg=(NumNegTot/MaxNumeri)*100;
cout << "Hai inserito " << NumPosTot << " numeri positivi, " << NumNegTot << " numeri negativi." << endl;
cout << "La percentuale di numeri positivi e' del " << PercentualePos << "%, mentre quella dei numeri negativi e' del " << PercentualeNeg << "%." << endl;
return 0;
}
|
thanks in advance :D