Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

C++ Apologies if this is way too basic...

ligetisplit

New Coder
I am trying to make a bunch of tiny cpp projects to practice the really basic concepts (my first time trying to learn, but I am committed),
is there a way to make this code simpler/easier to read, or perhaps even shorter while improving readability?

Thank you!
[CODE lang="cpp" title="nesting if statement dumb quiz"]//this program will use a nested if statement in the creation of a quiz
#include <iostream>
#include <string>


void Quiz()
{
std::cout << "Welcome to the first quiz, where you can only progress" << '\n' << "through the 5 questions by"
<< " correctly answering all questions correctly in sequence" << "\n\n\n";


std::string question1;
std::cout << "Who said the following: musicians are athletes of the small muscles?" << "\n\n";
std::getline(std::cin, question1);

if (question1 == "Hal Galpin")
{
std::cin.clear();
std::cout << "Correct! Now for the second question..." << '\n';
std::string question2;
std::cout << "What is the capital of the United Arab Emirates?" << "\n\n";
std::getline(std::cin, question2);

if (question2 == "Abu Dhabi")
{
std::cout << "Correct, once again. Please prepare for the third question... " << '\n';
std::string question3;
std::cout << "Who discovered the curvature of spacetime?" << "\n\n";
std::getline(std::cin, question3);

if (question3 == "Albert Einstein")
{
std::cout << "Congratulations... on to question 4, get out your thinking cap" << '\n';
std::string question4;
std::cout << "What is the first letter of the alphabet?" << "\n\n";
std::getline(std::cin, question4);

if (question4 == "a" || "A")
{
std::cout << "Wow, impressive work! Fifth question coming up..." << '\n';
std::string question5;
std::cout << "Who wrote the Declaration of Independence?" << "\n\n";
std::getline(std::cin, question5);

if (question5 == "Thomas Jefferson")
{
std::cout << "Congratulations, you are probably still stupid, but you passed the quiz!";
}else { std::cout << "Wow, you don't know this?"; }
}else { std::cout << "You seriously don't know this??"; }
}else { std::cout << "Come again?"; }
}else { std::cout << "Wow, typical American"; }
}else { std::cout << "Um, you're wrong, LOL"; }

}

int main()
{
Quiz();
return 0;
}[/CODE]
 
Hey, that's how I started, small programs to prove concepts :geek:

Main advice I have is to find structures that repeat themselves and try to extract them into a function. Note that on the other side of the coin is to make things too terse and that can also become unreadable.

Hold on, I'll come back with a suggestion.
 
[CODE lang="cpp" title="Quiz"]/*
*So overall decent code, i did make a few changes to look more clean in my opinion.
*
* - So first note is instead of using std:: in front of every cout, and cin, etc if you use the namespace std seen in line 11
* it automatically assumes the std in front of those functions.
- Second change I made is you had each cout ending in << "/n/n" which i thought looked a little messy so i swapped the "\n" into the
end of the string and ended each with endl; (just good practice)
- My last comment is just for assisting in readability i added a bunch of comments to help the reader by giving a little seperation and
helps the reader follow the code, this is more important in more complex code but its good practice to implement it in all of your code.
*/

//this program will use a nested if statement in the creation of a quiz
#include <iostream>
#include <string>

using namespace std; /*This makes all of you cout and cin a little cleaner as it allows you to remove the std::*/
void Quiz()
{
cout << "Welcome to the first quiz, where you can only progress\n"
<< "through the 5 questions by correctly answering all questions correctly in sequence\n"
<< "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl; /*I think it looks cleaner to use endl and the end of a cout rather than
using a bunch of \n as well as using some character to show page seperation*/

/*It is always good practice to add comments to you code to help with readability*/
/*Question 1*/
string question1;
cout << "Who said the following: musicians are athletes of the small muscles?" << endl;
getline(cin, question1);

if (question1 == "Hal Galpin")
{
cin.clear();
cout << "Correct! Now for the second question...\n" << endl;

/*Question 2*/
string question2;
cout << "What is the capital of the United Arab Emirates?\n" << endl;
getline(cin, question2);

if (question2 == "Abu Dhabi")
{
cout << "Correct, once again. Please prepare for the third question...\n" << endl;
/*Question 3*/
string question3;
cout << "Who discovered the curvature of spacetime?" << endl;
getline(cin, question3);

if (question3 == "Albert Einstein")
{
cout << "Congratulations... on to question 4, get out your thinking cap\n" << endl;

/*Question 4*/
string question4;
cout << "What is the first letter of the alphabet?" << endl;
getline(cin, question4);

if (question4 == "a" || "A")
{
cout << "Wow, impressive work! Fifth question coming up...\n" << endl;

string question5;
cout << "Who wrote the Declaration of Independence?" << endl;
getline(cin, question5);

/*Question 5*/
if (question5 == "Thomas Jefferson")
{
cout << "Congratulations, you are probably still stupid, but you passed the quiz!" <<endl;
}
else { cout << "Wow, you don't know this?" << endl; }
}
else { cout << "You seriously don't know this??" << endl; }
}
else { cout << "Come again?" << endl; }
}
else { cout << "Wow, typical American" << endl; }
}
else { cout << "Um, you're wrong, LOL" << endl; }

}

int main()
{
Quiz();
return 0;
}
[/CODE]
 
Maybe I can make one suggestion, and see how you can work with it. Nested ifs can be hard to follow but let's see if we can reduce something.

Each question has 4 things: the question, the expected answer, the response to a correct answer, and a response to a wrong answer. Let me suggest a function:

C:
bool askQuestion(string question, string correctAnswer, string correctRepsonse, string wrongResponse)
{
    string answer;
    cout << question;
    getline(cin, answer);
    if (answer == correctAnswer)
    {
        cout << correctReponse << endl;
        return true;
    }
    else
    {
        cout << wrongResponse << endl;
        return false;
    }
}

If you were to use this function to somehow replace the logic you have in the Quiz() function, it doesn't displace the strings in that function. I can suggest using some type of structure to store the question, answer, correct response, and wrong response. Then you can change the param to the function I suggested too ...

See how you go with that, and good luck!
 
OR the alternative would be to create a function per question:

C:
bool askQuestion1(void)
{
    string answer;
    cout << "Who said the following: musicians are athletes of the small muscles?" << endl;
    getline(cin, answer);

    if (answer == "Hal Galpin")
    {
        cout << "Correct! Now for the second question...\n" << endl;
        return true;
    }
    else
    {
        cout << "Um, you're wrong, LOL" << endl;
        return false;
    }
}
 
Hi there,

Welcome to Code Forum!

Seems like you are already in good hands, @Krusty the Senile and @SpicyHobo are pretty good with this stuff. As a person who is currently learning C++ right now, I'd have to say just make sure to add comments. Depending on how a developer uses comments, they can be used to tell another or future you on how your program works. This is key as it can help down the line if you ever have an issue. So add comments, explain your code.
 
Back
Top Bottom