Hey! Tips or Criticism?

Hello! I've been learning C++ for about 3 days now, and I was wondering if you guys had any tips on condensing this, or if you had any criticism on the way I code.

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
//  main.cpp
//  The JungKook Test
//
//  Created by Jeremy Lucero on 7/20/16.
//  Copyright © 2016 Jeremy Boii. All rights reserved.
//

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string Answer;
    double Point = 1;
    double Score = 0;
    double Total = 3;//Fix according to how many questions!!
    cout << "\t\tWelcome to the Jungkook Test!\nAre you ready?\n\n\n";
    // First Question
    cout << "1) How old is JungKook as of now in American age, prior to September 2016?\n";
    cin >> Answer;
    if (Answer == "18") {
        cout << "\nGood Job! You got that right you Jungkookster!\n";
        Score = Score + Point;
        cout << "\nNext question!\n";
    }
    if (Answer != "18"){
        cout << "\nAww, the right anwer was 18 years old";
        cout << "\nOh no! You got that EASY question wrong...\n\n\nHere's the next one :(\n\n";
    }
    // Second Question
    cout << "\n2) How tall is he in centimeters?\n";
    cin >> Answer;
    if (Answer == "178") {
        cout << "\nDang aren't you cray cray for the JungKookie?\nWell you got that right!\n\n";
        cout << "Next question!\n\n";
        Score = Score + Point;
    }
    if (Answer != "178") {
        cout << "\nSorry, but the correct answer was 178";
        cout << "\nThat was a pretty easy one, but who am I to judge? :|\n";
        cout << "\n*sigh* Next question...\n\n";
    }
    //Third Question
    cout << "\n3) What is Jungkook's hometown?\n";
    cin >> Answer;
    if (Answer == "Busan" || "busan") {
        cout << "Congratulations! You got that question!\nIt was a little hard, but wow good job!\n\n";
        Score = Score + Point;
    }
    //Add more questions if you want
    double Percent = Score/Total;
    cout << "Your total score was..." << Score << "/" << Total;
    if (Percent < 0.60 ) {
        cout << "\nYou got a " << Percent << "%\n";
        cout << "How'd you get an F man...\n\n";
        return 0;
    }
    if (0.60 <= Percent && Percent < 0.70) {
        cout << "\nYou got a " << Percent << "%\n";
        cout << "Wow... a D... I mean at least you didn't get a F...\n\n";
        return 0;
    }
    if (0.70 <= Percent && Percent < 0.80) {
        cout << "\nYou got a " << Percent << "%\n";
        cout << "Nice C, but you probably could've done better...\n\n";
        return 0;
    }
    if (0.80 <= Percent && Percent < 0.90) {
        cout << "\nYou got a " << Percent << "%\n";
        cout << "I mean a B isn't bad, but you should strive for better...\n\n";
        return 0;
    }
    if (0.90 <= Percent && Percent < 1.00) {
        cout << "\nYou got a " << Percent << "%\n";
        cout << "Ooooh you are a true JungKookster!! Keep being a happy BTS fan!\n\n";
        return 0;
    }
    if (Percent == 1.00) {
        cout << "\nYou got a 100%!!!!!!!!\n";
        cout << "You are worthy of being the president of the Army Fanbase!\n\n";
        return 0;
    }
}
Hi,
So there is no problem in your solution?
It is very cleanly written and easy to read.

As you develop your coding ability your code will, naturally, improve. But there's nothing wrong with what you've got.

Notice that it says 0.8% for everything under 100%. You might want to multiply by 100 for those to read 80%.

You can also reduce the number of calculations with an if..else if chain.
Repeated code can also be moved out of the conditional blocks.

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    cout << "\nYou got a " << (Percent * 100) << "%";
    if (Percent < 0.60) {
        cout << "\nHow'd you get an F man...\n\n";
    }
    else if (Percent < 0.70) {
        cout << "\nWow... a D... I mean at least you didn't get an F...\n\n";              
    }
    else if (Percent < 0.80) {
        cout << "\nNice C, but you probably could've done better...\n\n";
    }
    else if (Percent < 0.90) {
        cout << "\nI mean a B isn't bad, but you should strive for better...\n\n";
    }
    else if (Percent < 1.00) {
        cout << "\nOoooh you are a true JungKookster!! Keep being a happy BTS fan!\n\n";
    }
    else {
        cout << "!!!!!!!!\nYou are worthy of being the president of the Army Fanbase!\n\n";
    }
    return 0;

Hope this helps.
An addition to whats been said, a few more things:

There is a condensed way of writing this: Score = Score + Point;
And that is: Score += Point;

Line 48 is incorrect: if (Answer == "Busan" || "busan")
It should be: if (Answer == "Busan" || Answer == "busan")

You might also want to start getting used to doing without the using namespace std; and instead use the scope resolution operator like std::cout<< instead of cout <<

All in all, good code. Wish you best in learning further :)
Last edited on
Arslan7041
I'm also learning c++.
Why is using namespace std; not advisable?
It's not. It is just a hot-button issue with C++ pedants.

Whenever you using namespace anything; you are dumping every symbol in 'anything's namespace into the current namespace. This may cause unexpected collisions.

In large, production-level code, there is rarely any reason to do that.
In most other cases (anything you are creating right now) it really isn't an issue.
Why is using namespace std; not advisable?

Here's a good explanation of the issue.
http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice

As Duoas said, for the small programs here, it doesn't matter.
Topic archived. No new replies allowed.