Hello everyone,
I am trying to teach my self C++, and I am having trouble with the following code:
#include <iostream>
#include <cstring>
#include <iomanip>
#include <cmath>
using namespace std;
using std::setprecision;
using std::setw;
int main()
{
const int NUMBER_OF_STATES = 10;
const int NUMBER_OF_COLUMNS = 30;
int count = 0;
char *capitalArray[NUMBER_OF_STATES][NUMBER_OF_COLUMNS] =
{
{"Alabama", "Montgomery"},
{"Alaska", "Juneau"},
{"Arizona", "Phoenix"},
{"Arkansas", "Little Rock"},
{"California", "Sacramento"},
{"Colorado", "Denver"},
{"Connecticut", "Hartford"},
{"Delaware", "Dover"},
{"Florida", "Tallahassee"},
{"Georgia", "Atlanta"}
};
for (int i = 0; i < NUMBER_OF_STATES; i++) {
cout << "Enter the capital of " << capitalArray[0] << ":" << endl;
char capital;
cin >> capital;
bool isInArray = true;
if (capital != *capitalArray[1]){
isInArray = false;
}
if (isInArray){
cout << "Correct Answer" << endl;
count++;
}
else if (!isInArray){
cout << "Wrong Answer" << endl;
}
}
cout << "The number of correct answers is: " << count << endl;
return 0;
}
Basically, whever I enter an answer longer than 2 characters it skips a random numbe of questions and marks them wrong. I'm not sure where the problem is, can someone help? Thanks
I am trying to teach my self C++, and I am having trouble with the following code:
#include <iostream>
#include <cstring>
#include <iomanip>
#include <cmath>
using namespace std;
using std::setprecision;
using std::setw;
int main()
{
const int NUMBER_OF_STATES = 10;
const int NUMBER_OF_COLUMNS = 30;
int count = 0;
char *capitalArray[NUMBER_OF_STATES][NUMBER_OF_COLUMNS] =
{
{"Alabama", "Montgomery"},
{"Alaska", "Juneau"},
{"Arizona", "Phoenix"},
{"Arkansas", "Little Rock"},
{"California", "Sacramento"},
{"Colorado", "Denver"},
{"Connecticut", "Hartford"},
{"Delaware", "Dover"},
{"Florida", "Tallahassee"},
{"Georgia", "Atlanta"}
};
for (int i = 0; i < NUMBER_OF_STATES; i++) {
cout << "Enter the capital of " << capitalArray[0] << ":" << endl;
char capital;
cin >> capital;
bool isInArray = true;
if (capital != *capitalArray[1]){
isInArray = false;
}
if (isInArray){
cout << "Correct Answer" << endl;
count++;
}
else if (!isInArray){
cout << "Wrong Answer" << endl;
}
}
cout << "The number of correct answers is: " << count << endl;
return 0;
}
Basically, whever I enter an answer longer than 2 characters it skips a random numbe of questions and marks them wrong. I'm not sure where the problem is, can someone help? Thanks