Hello ZunLee,
In addition to what
Ganado has said.
Although you did not mention it your code leads me to believe that you are using some version of MSVS.
This is an idea of what you code could be.
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
|
#include <iostream>
#include <string>
#include <cstdlib> // <--- For "rand()" and "srand()".
#include <ctime> // <--- For "time()".
using namespace std;
int MajorIntervals()
{
char KeyOfC[] = { 'E', 'e', 'G', 'g', 'B', 'b' };
int randomQuestion{};
string third, fifth, seventh;
randomQuestion = rand() % 3 + 1;
switch (randomQuestion)
{
//New question
case 1:
cout << "What's the major third(3rd) of "; // <--- Question does not match format of case 2 and case 3.
std::getline(std::cin, third); // <--- Changed.
while (third[0] != KeyOfC[0] && third[0] != KeyOfC[1])
{
cout << "\n wrong answer, please try again...:\n\n"; // <--- Changed.
cout << "What's the major third(3rd) of "; // <--- Added.
std::getline(std::cin, third);
}
cout << "\n That's correct. Well done!\n\n";
return MajorIntervals(); // <--- Recursive function call with no way out.
|
In VS "iostream" will eventually include "cstdlib" along with some others.
DO NOT COUNT on the being the norm. Not all compilers have their header files set up the same as VS.
It is better to include what you need and let the header guards keep the code from being included more than once.
I do not believe it makes any difference, but the line
using namespace std;
usually follows the include files. It is not needed for any of the standard include files, but for the code that follows. It is also best not to use this line, but that is a different topic in its-self.
To reiterate what
Ganado said. The colon at the end of line 5, in your second code, or line 8, in my code, makes a very nice prototype which you do not need because the function is compiled before "main".
Had the function been defined after "main" you would need the prototype so the compiler would know what to expect.
I removed all the parameters of the function. These variables are only used in the function and are better defined there unless you have some plan on using them later in which case the strings should be passed by reference, so that any changes made in the function would be reflected back in "main", but you are not showing that at this time.
It is also a good idea to initialize your variables when defined so they do not contain a garbage value. The exception is "std::string"s, "std::vector"s and other containers which have a zero size when defined and do not need initialized.
The line choosing a random number is OK, but does have its limitations.
In "case 1" the comment on the prompt covers that.
Using "std::getline" is a better choice as you have defined your variables as "std::string"s. This will leave the input buffer empty when finished whereas the formatted input of
cin >> third;
could leave something in the input buffer causing the next "cin" to not wait for input, but use what is in the buffer. This will cause the while loop to continue until the input buffer is emptied, so you could see several error messages when you do not need to.
The while condition is an alternative to changing the strings to "char". By accessing just one element of the string you are comparing a "char" to a "char". Also it will not matter how long the string is because you are only comparing the first element of the string.
The way you have it set up it is best to re-prompt while inside the while loop.
They are little things, but look at the way I used spaces to indent the error message and the correct response compared to the prompts.
Also there are several places in your code where you have extra blank lines that are not needed and other places where you do. It is a small bit of code, but notice where I used blank lines to break up the code. This makes it easier to read and follow.
The last line of the case statement is a recursive call to the function. This may do what you want, but there is no way out. Also the function promises to return an "int" but it does not. You are trying to return the return value of the function, but the function never returns any "int" variable or value. This tends to create an endless loop.
What you see here for "case 1" can be applied to "case 2" and "case 3".
In "main" other than removing the variable definitions I added:
srand(static_cast<unsigned int>(time(nullptr)));
. First if you are going to use "rand()" you need to seed the RNG before calling "rand". Otherwise it will seed the RNG with 1 making sure that all your calls to "rand" produce the same random numbers in the same order. This line of code may seem a bit much, but it is the best way to write it.
If you are going to use "rand()" this video is worth watching.
https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
And this may be worth the read.
https://web.archive.org/web/20180123103235/http://cpp.indi.frih.net/blog/2014/12/the-bell-has-tolled-for-rand/
Andy