Am I programming in a wrong way?

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
Last edited on
1. don't using namespace std;, cuz it will flood a large quantity of names. your should using std::cout; using std::end; ... or write std::cout explicitly
Your looks pretty good. 2 suggestions
1. instead of cout << endl; if you just want to skip a line you could use cout << "\n"; Not sure how big of a difference it makes.
2. It would nice to see you comment on parts of your program on what its doing. For a simple program like this it is not that important but when you get larger programs they can be very helpful and their just a good habit to get in to using.

Oh one other thing the difference it makes is very small but something like NumNegTot=NumNegTot+1; you could also write at as NumNegTot += 1;
Last edited on
Topic archived. No new replies allowed.