Question

C++, data structure Write a program that plays a game called "guess the state, guess the...

C++, data structure

Write a program that plays a game called

"guess the state, guess the capital".

How do you play? The program randomly selects a state or a state capital. The program asks the player to guess the state's capital (or the capital's state). The program reads the user's guess; and, tells the user if its guess is right, or if the guess is wrong, tells the user that its guess is wrong and displays the correct answer.

Before the game begins, the program opens the states and capitals text file, "State&Capitals.txt", and reads its contents into some data structure or data structures.

The file's format is

<state or territory name 1>

<capital 1>

<state or territory name 2>

<capital 2>

<state or territory name n>

<capital n>

You must use dynamic structures to write your program.

Upload your source code to Canvas. Do not upload any exe file or system files. Upload only the files your write.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// State&Capitals.txt

Alabama
Montgomery
Alaska
Juneau
Arizona
Phoenix
Arkansas
Little Rock
California
Sacramento
Colorado
Denver
Connecticut
Hartford
Delaware
Dover
Florida
Tallahassee
Georgia
Atlanta
Hawaii
Honolulu
Idaho
Boise
Illinois
Springfield
Indiana
Indianapolis
Iowa
Des Moines
Kansas
Topeka
Kentucky
Frankfort
Louisiana
Baton Rouge
Maine
Augusta
Maryland
Annapolis
Massachusetts
Boston
Michigan
Lansing
Minnesota
Saint Paul
Mississippi
Jackson
Missouri
Jefferson City
Montana
Helena
Nebraska
Lincoln
Nevada
Carson City
New Hampshire
Concord
New Jersey
Trenton
New Mexico
Santa Fe
New York
Albany
North Carolina
Raleigh
North Dakota
Bismarck
Ohio
Columbus
Oklahoma
Oklahoma City
Oregon
Salem
Pennsylvania
Harrisburg
Rhode Island
Providence
South Carolina
Columbia
South Dakota
Pierre
Tennessee
Nashville
Texas
Austin
Utah
Salt Lake City
Vermont
Montpelier
Virginia
Richmond
Washington
Olympia
West Virginia
Charleston
Wisconsin
Madison
Wyoming
Cheyenne
American Samoa
Pago Pago
Guam
Hagåtña
Northern Mariana Islands
Saipan
Puerto Rico
San Juan
U.S. Virgin Islands
Charlotte Amalie

___________________________


#include <fstream>
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;

int main() {
srand(time(NULL));  
//defines an input stream for the data file  
ifstream dataIn;
string stateName,capital;
int cnt=0;
char ch;
  
//Opening the input file
dataIn.open("State&Capitals.txt");
//checking whether the file name is valid or not
if(dataIn.fail())
{
   cout<<"** File Not Found **";
return 1;
}
else
{
   while(getline(dataIn,stateName))
   {
       getline(dataIn,capital);
       cnt++;
   }
   dataIn.close();
  
   // Creating array dynamically
string* stateNames = new string[cnt];
string* capitals = new string[cnt];
  
//Opening the input file
dataIn.open("State&Capitals.txt");
   for(int i=0;i<cnt;i++)
   {
       getline(dataIn,stateName);
       getline(dataIn,capital);
      
       stateNames[i]=stateName;
       capitals[i]=capital;
     
   }
dataIn.close();
while(true)
{
   int random=rand()%(cnt+1);
cout<<"What is the capital for "<<stateNames[random]<<" ?"<<endl;
getline(cin,capital);
  
  
   if(capitals[random].compare(capital)==0)
   {
       cout<<"Correct!"<<endl;
   }
   else
   {
       cout<<"Wrong."<<endl;
       cout<<"The Correct answer is "<<capitals[random]<<endl;
   }
   cout<<"\nDo you Want to continue(Y/N):";
cin>>ch;

if(ch=='y'||ch=='Y')
{
cin.ignore();
   continue;

}else
{
break;
}
   }

}

  

  
  
return 0;
}
____________________________

Output:

_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
C++, data structure Write a program that plays a game called "guess the state, guess the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For this lab, write a program that lets the user enter a state and the program...

    For this lab, write a program that lets the user enter a state and the program returns that state's capital, stored in a dictionary Requirements: Start by hard coding in all of the states and their capitals the user should be able to enter the name of a state or ask for all states to be printed (one key/value pair per line) If the state is in the dictionary, print its state capital in the form: "The capital of Vermont...

  • Football Game Scores Write a C++ program that stores the following data about a football game...

    Football Game Scores Write a C++ program that stores the following data about a football game in a structure: Field Name Description visit_team string (name of visiting team) home_score int visit_score int The program should read the number of games from a data file named “games.txt”, dynamically allocate an array of structures for the games using one structure for each game, and then read the information for each game from the same file (visiting team, home score, visiting team’s score)....

  • Write a program (C++) that shows Game sales. The program should use a structure to store...

    Write a program (C++) that shows Game sales. The program should use a structure to store the following data about the Game sale: Game company Type of Game (Action, Adventure, Sports etc.) Year of Sale Sale Price The program should use an array of at least 3 structures (3 variables of the same structure). It should let the user enter data into the array, change the contents of any element and display the data stored in the array. The program...

  • Python 3 **11.40 (Guess the capitals) Write a program that repeatedly prompts the user to enter...

    Python 3 **11.40 (Guess the capitals) Write a program that repeatedly prompts the user to enter a capital for a state. Upon receiving the user input, the program reports whether the answer is correct. Assume that 50 states and their capitals are stored in a two- dimensional list, as shown in Figure 11.13. The program prompts the user to answer all the states’ capitals and displays the total correct count. The user’s answer is not case sensitive. Implement the program...

  • Write a C++ code for this question- Write a program with a method that plays the...

    Write a C++ code for this question- Write a program with a method that plays the guess a number game. The program should allow the user to pick a number between 1 and 1000 in his head. The method should guess the user's number in a minimal amount of attempts. The method should ask the user "is the number greater than or less than the number    " and the program gives a particular number. The user in some way just...

  • Write a JAVA program that plays a number guessing game with the user. A sample run...

    Write a JAVA program that plays a number guessing game with the user. A sample run for the game follows. User input is shown in boldface in the sample run. Welcome to the game of Guess It! I will choose a number between 1 and 100. You will try to guess that number. If your guess wrong, I will tell you if you guessed too high or too low. You have 6 tries to get the number. OK, I am...

  • Write a C++ program to play the number guessing game with user as follows.

    Write a C++ program to play the numberguessing game with user as follows.The user determines a number between 1~100.The program has 4 tries to guess the number byproviding a range (low, high) on each try.The user tells the program whether the number isbelow, within, or above the range.The program announces a game loss after 4 incorrecttries.

  • c++ launguage please help The game Pico Fermi Bagel is a number guessing game. The computer...

    c++ launguage please help The game Pico Fermi Bagel is a number guessing game. The computer picks a secret number: the secret number must be 3 digits, none of the digits can be the same, and it cannot start with a zero. 104 is OK, but 091 and 212 are not. The user tries to guess the secret number - if none of the digits in the user's number are in the secret number, then the program replies with BAGEL...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

  • C++ program: Write an address book program that will accomplish the following: 1. Read name and...

    C++ program: Write an address book program that will accomplish the following: 1. Read name and address data from the user from the keyboard. 2. While reading the names and addresses, put the names into an appropriate data structure. 3. After reading names and addresses, allow user to search for names and change the names or addresses in the container data structure. 4. Allow user to write out the container data structure to a comma delimited file. The input file...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT