Question

In: Computer Science

C++ program, I'm a beginner so please make sure keep it simple Write a program to...

C++ program, I'm a beginner so please make sure keep it simple

Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution.

Input File.txt:

Cincinnati 27, Buffalo 24
Detroit 31, Cleveland 17
Kansas City 24, Oakland 7
Carolina 35, Minnesota 10
Pittsburgh 19, NY Jets 6
Philadelphia 31, Tampa Bay 20
Green Bay 19, Baltimore 17
St. Louis 38, Houston 13
Denver 35, Jacksonville 19
Seattle 20, Tennessee 13
New England 30, New Orleans 27
San Francisco 32, Arizona 20
Dallas 31, Washington 16

output File:

Cincinnati over Buffalo by 3
Detroit over Cleveland by 14
Kansas City over Oakland by 17
Carolina over Minnesota by 25
Pittsburgh over NY Jets by 13
Philadelphia over Tampa Bay by 11
Green Bay over Baltimore by 2
St. Louis over Houston by 25
Denver over Jacksonville by 16
Seattle over Tennessee by 7
New England over New Orleans by 3
San Francisco over Arizona by 12
Dallas over Washington by 15

Solutions

Expert Solution

//c++ program to read txt file
#include <Bits/stdc++.h>
#include <cstring>
using namespace std;

int main()
{
  const char file_name[] = "test.txt";//give location of your txt file 
 char buffer[16], winner[32], loser[32]; //variables are defined .
  int winning _score, lossing_score;

   ifstream fin(file_name);//conditions if file dosent open the it will give error message :Unable to open file
   if (!fin)
  {
   cout<< "Unable to open file " << file_name << endl;
   exit(1);
   }

   while (!fin.eof())
  {
  fin >> winner;
  if (fin.eof()) break;
   fin >> buffer;
   if (strchr(buffer,','))
   {
   winning _score = atoi(strtok(buffer,","));
   }
   else
   {
   strcat(winner," ");
   strcat(winner,buffer);
   fin >> buffer;
   winning _score = atoi(strtok(buffer,","));//atoi( ) function hepls to convert string to integer.
  }
  fin >> loser;
   fin >> buffer;
   if (isdigit(buffer[0]))//it wii get digits from the txt file.
   {
   lossing_score = atoi(buffer);
  }
   else
  {
   strcat(loser," ");//strcat( ) function will help to append the copy of the string to end of the string 
    strcat(loser,buffer);
   fin >> buffer;
   lossing_score = atoi(buffer);
   }

 cout << winner << " over " << loser << ' ' << " by " <<winning _score - lossing_score << endl;
   }

return 0;
}

Here the output screen:

if you have any problem regarding this question then feel frere ro ask in comment section.


Related Solutions

USE C++ and please keep program as simple as possible and also comment so it is...
USE C++ and please keep program as simple as possible and also comment so it is easy to understad Create a structure called time. Its three members, all type int, should be called hours, minutes, and seconds. Write a program that prompts the user to enter a time value in hours, minutes, and seconds. This should be in 12:59:59 format. This entire input should be assigned first to a string variable. Then the string should be tokenized thereby assigning the...
Please make it simply and easy for a beginner to follow.. -Write in C++ -Use Char...
Please make it simply and easy for a beginner to follow.. -Write in C++ -Use Char library functions -Must show that is runs Write a function that accepts a string representing password and determines whether the string is a valid password. A valid password as the following properties: 1. At least 8 characters long 2. Has at least one upper case letter 3. Has at least one lower case letter 4. Has at least one digit 5. Has at least...
Please make it simply and easy for a beginner to follow -Write in C++ -Use Char...
Please make it simply and easy for a beginner to follow -Write in C++ -Use Char library functions Write a function that accepts a string representing password and determines whether the string is a valid password. A valid password as the following properties: 1. At least 8 characters long 2. Has at least one upper case letter 3. Has at least one lower case letter 4. Has at least one digit 5. Has at least on special characte
PROGRAM LANGUAGE IN C, PLEASE KEEP IT SIMPLE EVEN IF IT IS REDUNDANT In this problem...
PROGRAM LANGUAGE IN C, PLEASE KEEP IT SIMPLE EVEN IF IT IS REDUNDANT In this problem you will take a list of names from the user and sort them alphabetically using selection sort. The code for selection sort for integer is available in the "Lab and Weekly Coding Solution module" in webcourses. Ask the user how many names the user wants to input. Let's say the number be N. Then take N number of names and put them in an...
Use C++ please You will be building a linked list. Make sure to keep track of...
Use C++ please You will be building a linked list. Make sure to keep track of both the head and tail nodes. (1) Create three files to submit. PlaylistNode.h - Class declaration PlaylistNode.cpp - Class definition main.cpp - main() function Build the PlaylistNode class per the following specifications. Note: Some functions can initially be function stubs (empty functions), to be completed in later steps. Default constructor (1 pt) Parameterized constructor (1 pt) Public member functions InsertAfter() - Mutator (1 pt)...
PROGRAM LANGUAGE IN C NOT C# or C++ KEEP IT SIMPLE EVEN IF IT IS REDUNDANT...
PROGRAM LANGUAGE IN C NOT C# or C++ KEEP IT SIMPLE EVEN IF IT IS REDUNDANT PLEASE. Problem: Driving Function driving(): Write a function driving(), that updates the odometer and fuel gauge of a car. The function will take in a reference to the variables storing the odometer and fuel gauge readings, along with a double representing the miles per gallon (mpg) the car gets and the number of miles the driver intends to go. The function should update the...
THIS IS EXAM REVIEW, so please make sure to show the work and make sure the...
THIS IS EXAM REVIEW, so please make sure to show the work and make sure the work is correct. In all tests of hypothesis use the 5% level of significance unless told otherwise. In all confidence interval problems use 95% confidence unless told otherwise. SHOW YOUR WORK. 2. Test scores for a mathematics course have a normal distribution with a mean of 76 and a standard deviation of 12. a.What proportion of scores will be between 70 and 90? b....
Please write in beginner level PYTHON code! Your job is to write a Python program that...
Please write in beginner level PYTHON code! Your job is to write a Python program that asks the user to make one of two choices: destruct or construct. - If the user chooses to destruct, prompt them for an alternade, and then output the 2 words from that alternade. - If the user chooses construct, prompt them for 2 words, and then output the alternade that would have produced those words. - You must enforce that the users enter real...
Write a program using c++. Write a program that uses a loop to keep asking the...
Write a program using c++. Write a program that uses a loop to keep asking the user for a sentence, and for each sentence tells the user if it is a palindrome or not. The program should keep looping until the user types in END. After that, the program should display a count of how many sentences were typed in and how many palindromes were found. It should then quit. Your program must have (and use) at least four VALUE...
Simple code please thats easy to follow. C++ Write a program that can be used to...
Simple code please thats easy to follow. C++ Write a program that can be used to compare Insertion Sort, Merge Sort and Quick Sort. Program must: Read an array size from the user, dynamically an array of that size, and fill the array with random numbers Sort the array with the Insertion Sort, MergeSort and QuickSort algorithms studied in class, doing a time-stamp on each sort. Use your program to measure and record the time needed to sort random arrays...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT