Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pointers in C++, skipping results 2

Status
Not open for further replies.

JVasq13

MIS
Apr 11, 2008
3
0
0
US
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
 
Your capital variable is a char, which means it holds a single character.

In C, you hold a string (which is a sequence of characters) in a char array, which is what you are using for your capitalArray.

However, if you're learning C++ you should use a C++ string for that array and for reading in the capital from the user. To use a C++ string, add #include <string> and make your variable string instead of char or char*.

You should also consider getting a newer learning source that teaches modern C++ like the string class, instead of C style code like what you have here.
 
Thanks for the help, now I'm trying to figure out how to do what you suggested. This is the kind of example the book gives:

// Prompt the user to enter a string
cout << "Enter a string: ";
char s[80];
cin.getline(s,80);

I'm not sure how to apply this concept to the excercise. The book doesn't say anything about pointers and 2 dimensional arrays, which makes it really hard when trying to do the practice problems.
 
That code will correctly read in a string (that is less than 80 characters) into the variable s. I really think you should be using C++ strings, but if you have to or want to continue with the book you have then you just need to use that code instead of char capital and cin >> capital.

char capital; declares a char variable named capital that holds a character. char s[80]; declares a char array variable that holds up to 79 characters. The second version is what you need for your problem.

cin >> capital; reads in a character from cin and stores it in the variable capital. cin.getline(s, 80); reads a sequence of characters until the end of the line and stores that sequence in the variable s. Again, you want the second version.

All the stuff you have with the states and capitals in the big list should work, you don't have to change it.
 
Once again, thanks for your help. This is what I have now:

//Jonathan J. Vasquez
//Assignment 7
// Calculating parking cost
// This program calculate parking charges for the 10 customers that parked the day before.
#include <iostream>
#include <cstring>
#include <string>
#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 = 3;
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[15];
cin.getline(capital, 15);

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;
}


but when I run the program I get an error saying MSVCP90D.dll was not found. i reinstalled Visual C++ with no joy. I did some research on the MS knowlege base and tried everything they suggested but nothing worked. I think the code should be fine now, except that I have not been able to test it
 
 http://i42.photobucket.com/albums/e303/metalero111/cerror2.jpg
Not sure why it's not executing, but you've got another problem...

Code:
if (capital != capitalArray[i][1]){
should be
Code:
if ( strcmp( capital, capitalArray[i][1] ) == 0 ) {
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top