Am I using std:: correctly?

Hello.

I usually use: 'using namespace std', however: today I tried not using the command, and added the prefix std::, to the commands that require it.

Am I doing it correctly?

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
#include <iostream>
#include <string>
#include <sstream>

int main()

{
    unsigned int user_total_valid = 0;
    unsigned int user_total_0 = 0;
    std::string use_again;
    std::string user_mark_s;
    long double user_mark = 0;
    std::string user_total_s;
    long double user_total = 0;
    long double quotient = 0;
    long double res = 0;

    do
    {
        std::cout << std::endl;
        std::cout << "Percentage calculator." << std::endl;

        std::cout << "Input the mark:" << std::endl;
            getline ( std::cin, user_mark_s );
            std::stringstream( user_mark_s ) >> user_mark;

        std::cout << "Input the total:" << std::endl;
            getline ( std::cin, user_total_s );
            std::stringstream( user_total_s ) >> user_total;

        if ( user_total >-0.00000000001 && user_total < 0.00000000001 )
        {
            user_total_0 = 1;
            std::cout << "Cannot divide by zero." << std::endl;
        }

        while ( user_total_0 != 1 && user_total_valid != 1 )
        {
            user_total_valid = 1;
            quotient = user_mark/user_total;
            res = quotient * 100;

            std::cout << user_mark << "/" << user_total << " = " << quotient << "." << std::endl;
            std::cout << quotient << " x " << "100" << " = " << res << "." << std::endl;
            std::cout << "Therefore: the mark is " << res << "%" << " of the total." << std::endl;
        }
        std::cout << "Input 'y' to use this program again." << std::endl;
            std::cin >> use_again;
        std::cin.ignore();

        user_total_valid = 0;
        user_total_0 = 0;
     }
     while ( use_again == "y" || use_again == "Y" );
}
Yes.
Thank you, mutexe, for the quick reply.
that is cool, but a lot of repeating
If you are still not confident in your ability to both stay sane and repeatedly type std:: over and over, you can explicitly use the specific things you want:
1
2
3
using std::cout;
using std:endl;
using std::cin;
The goal is just to never ever write "using namespace std;" or similar, because it's like opening the floodgates for your friends - you have no idea about the massive flood of other things that come in.
I'd advise using std:: with any standard function:

std::getline(...); *yes, this works*

It makes it obvious that you're using something standard, not some "getline(...)" function someone else may've made, that is overriding the local scope.
I usually recommend the std:: method. I recommend always keeping the using declarations as local as possible. For example if I'm using cout and endl in a function quite a lot I might put the using std::cout; statement inside that function. This limits the scope of the using declaration to that function.
1
2
3
4
5
6
7
void display()
{
   using std::cout;
   using std::endl;

... The rest of the function.
}

Topic archived. No new replies allowed.