Question

i have two issues that i need help. 1- that in case three and two only...

i have two issues that i need help.

1- that in case three and two only one line is being read from the file

2- my case 4 is not working correctly as it should as the output is only

" Enter 1 for Trump, 2 for Warren:"

#include <iostream>
#include <cctype> // For the letter checking functions
#include <fstream> // For file input
#include <iomanip> // For setw
#include <ctime>
#include <cstdlib> // For exit and abs
#include <errno.h>
#include <string>
//#include <string.h>

using namespace std;

int main() {
   //import random;
     
   int option;                   //Option user select is saved in this variable.
   int print_lines = 1;       //Lines to print is selected by a user.
   int trump_lines = 0;       //Total lines in a trump file.
   int warren_lines = 0;       //Total lines in a warren file.
   int random_start = 1;       //Random start location to read from a file.
   int line_num = 0;
int userInput; //Current line number reading from a file.
int numberOfQuizzes; // How many times a quiz has been taken.
int numberOfQuizCorrect; // How many times the user got the quiz correct.
   string read_line;           //Read line from a file is stored in this string.
   ifstream trump;               //trump file object.
   trump.open("Trump.txt");   //Open a trump file.
   if (!trump.is_open())
   {
       cout << "Trump file is not opened.\n";
       cout << "Error code is: " << errno << endl;
       return -1;
   }
   getline(trump, read_line);
   trump_lines = stoi(read_line); //Assign total lines in a trump file.

   ifstream warren;
   warren.open("Warren.txt");
   if (!warren.is_open())
   {
       cout << "Warren file is not opened.\n";
       cout << "Error code is: " << errno << endl;
       return -1;
   }
   getline(warren, read_line);
   warren_lines = stoi(read_line);   //Assign total lines in a warren file.
   //Initialize Random number generator.
   srand(time(0));

   while(true)
   {
       cout << " Program 2: Rally Songs "<<endl;
cout <<"Select from the following:\n";
       cout << " 1. Set number of lines to display\n";
       cout << " 2. Get random lines from Trump.txt\n";
       cout << " 3. Get random lines from Warren.txt\n";
       cout << " 4. Rally song quiz\n";
       cout << " 5. Exit the program\n";
cout << "Your choice --> ";   
       //cout << "Choose One of the above option.\n";
       cin >> option;

       switch (option)
       {
       case 1:
           cout << "Enter number of lines to print.\n";
           cin >> print_lines;
           break;
       case 2:

           random_start = rand() % (trump_lines - print_lines);
           line_num = 0;
           while (line_num != random_start && getline(trump, read_line))
           {
               line_num++;
           }
           for (int i = 0; i < print_lines; i++)
           {
               getline(trump, read_line);
               cout << read_line << endl;
           }
           trump.seekg(0);           //Set file pointer to start of a file.
           //cout << getline(trump, read_line) << endl;
           break;

       case 3:
           random_start = rand() % (warren_lines - print_lines);

           line_num = 0;
           while (line_num != random_start && getline(warren, read_line))
           {
               line_num++;
           }
           for (int i = 0; i < print_lines; i++)
           {
               getline(warren, read_line);
               cout << read_line << endl;
           }
           warren.seekg(0);               //Set file pointer to start of a file.
           break;
       case 4:

// Get random lines from a mystery candidate, and prompt for user guess
if( rand() % 2 == 0) {
read_line = "Trump.txt";
}
else {
read_line = "Warren.txt";
}
// Get the user guess of who it is
cout << endl;
cout << "Enter 1 for Trump, 2 for Warren: ";
cin >> userInput;
  
cout << endl;
  
// Compare user guess to correct answer and update quiz score.
numberOfQuizzes++; // Increment the number of times the quiz has been taken
// Give a point if quiz answer was correct
if( (userInput == 1 ?: trump_lines == 'Trump.txt' ) ||
(userInput == 2 ?: warren_lines == 'Warren.txt' )
) {
cout << "Correct! ";
numberOfQuizCorrect++;
}
else {
cout << "Wrong! ";
}
           return 0;

       }
          

   }
      
}

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

/******************************************************************************

Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/
#include <iostream>
#include <cctype>       // For the letter checking functions
#include <fstream>       // For file input
#include <iomanip>       // For setw
#include <ctime>
#include <cstdlib>       // For exit and abs
#include <errno.h>
#include <string>

using namespace std;

int main ()
{
int option;           //Option user select is saved in this variable.
int print_lines = 1;       //Lines to print is selected by a user.
int trump_lines = 0;       //Total lines in a trump file.
int warren_lines = 0;       //Total lines in a warren file.
int random_start = 1;       //Random start location to read from a file.
int line_num = 0;
int userInput;       //Current line number reading from a file.
int numberOfQuizzes;       // How many times a quiz has been taken.
int numberOfQuizCorrect;   // How many times the user got the quiz correct.
string read_line;       //Read line from a file is stored in this string.
ifstream trump;       //trump file object.
trump.open ("Trump.txt");   //Open a trump file.
if (!trump.is_open ())
{
cout << "Trump file is not opened.\n";
cout << "Error code is: " << errno << endl;
return -1;
}
//getline (trump, read_line);
//trump_lines = stoi (read_line);
//Assign total lines in a trump file.
while (getline(trump, read_line))
trump_lines++; //This should be the logic for total lines

ifstream warren;
warren.open ("Warren.txt");
if (!warren.is_open ())
{
cout << "Warren file is not opened.\n";
cout << "Error code is: " << errno << endl;
return -1;
}
//getline (warren, read_line);
//warren_lines = stoi (read_line);  
//Assign total lines in a warren file.
while (getline(warren, read_line))
warren_lines++; //This should be the logic for total lines
//Initialize Random number generator.
srand(time(0));

while (true)
{
cout << " Program 2: Rally Songs " << endl;
cout << "Select from the following:\n";
cout << " 1. Set number of lines to display\n";
cout << " 2. Get random lines from Trump.txt\n";
cout << " 3. Get random lines from Warren.txt\n";
cout << " 4. Rally song quiz\n";
cout << " 5. Exit the program\n";
cout << "Your choice --> ";
cin >> option;
switch (option)
   {
   case 1:
   cout << "Enter number of lines to print.\n";
   cin >> print_lines;
   break;
   case 2:
   trump.seekg(0);
   //Correrction in printing random lines
   for(int i=0;i< print_lines;i++)
   {
   random_start = rand() % trump_lines;
   line_num=0;
while(getline(trump, read_line))
{
line_num++;
if(line_num == random_start)
{
cout << read_line<<endl;
}
}
   }
// random_start = rand () % (trump_lines - print_lines);
   // line_num = 0;
   // while (line_num != random_start && getline (trump, read_line))
   // {
   // line_num++;
   // }
   // for (int i = 0; i < print_lines; i++)
   // {
   // getline (trump, read_line);
   // cout << read_line << endl;
   // }
   trump.seekg(0);   //Set file pointer to start of a file.
   break;
   case 3:
   warren.seekg(0);
   //Correrction in printing random lines
   for(int i=0;i< print_lines;i++)
   {
   line_num=0;
   random_start = rand() % warren_lines;
while(getline(warren, read_line))
{
line_num++;
if(line_num == random_start)
{
cout << read_line<<endl;
}
}
   }
   // random_start = rand () % (warren_lines - print_lines);
// line_num = 0;
   // while (line_num != random_start && getline (warren, read_line))
   // {
   // line_num++;
   // }
   // for (int i = 0; i < print_lines; i++)
   // {
   // getline (warren, read_line);
   // cout << read_line << endl;
   // }
   warren.seekg (0);   //Set file pointer to start of a file.
   break;
   case 4:
// Get random lines from a mystery candidate, and prompt for user guess
int ans;
   if (rand () % 2 == 0)
   {
   //read_line = "Trump.txt";
   ans=1;//Added additional variable to check for option
   getline(trump, read_line);
   cout<<read_line<<endl;
   }
   else
   {
   //read_line = "Warren.txt";
   ans=2;
   getline(warren, read_line);
   cout<<read_line<<endl;
   }
// Get the user guess of who it is
   cout << endl;
   cout << "Enter 1 for Trump, 2 for Warren: ";
   cin >> userInput;
   cout << endl;
// Compare user guess to correct answer and update quiz score.
   numberOfQuizzes++;   // Increment the number of times the quiz has been taken
// Give a point if quiz answer was correct
   // if ((userInput == 1 ? : trump_lines == 'Trump.txt') ||
   // (userInput == 2 ? : warren_lines == 'Warren.txt'))
   if((userInput==1 && ans==1) ||(userInput==2 && ans==2))//Checking if userInput is correct
   {
   cout << "Correct! ";
   numberOfQuizCorrect++;
   }
   else
   {
   cout << "Wrong! ";
   }
   return 0;
   }
}
}

You had some logic errors which is corrected

Add a comment
Know the answer?
Add Answer to:
i have two issues that i need help. 1- that in case three and two only...
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
  • please c++ with functions *Modify the Guessing Game Write the secret number to a file. Then...

    please c++ with functions *Modify the Guessing Game Write the secret number to a file. Then write each user guess and answer to the file. (on separate lines) Write the number of guesses to the file at the end of the game. After the game is finished, ask the user if they want to play again. If 'n' or 'N' don't play again, otherwise play again! NOTE: your file should have more than one game in it if the user...

  • I am trying to write this code which asks "Write a program that ask the user,...

    I am trying to write this code which asks "Write a program that ask the user, the question: What is a^b? //The program generates two signed integers and gives the user four attempts to get the correct answer //a=- , b = + //a= + , b = - " So far this what I wrote. I am not sure how to do this correctly. #include<iostream> #include<cstdlib> #include<ctime> using namespace std; int main() {    srand(time(0));    int guess,a,ans,b, k;...

  • C++ getline errors I am getting getline is undefined error messages. I would like the variables...

    C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...

  • I am reading a text file and trying to store the information into an array so...

    I am reading a text file and trying to store the information into an array so I can use this in different parts of my program. So a dynamic array. So far I have been able to retrieve the information from the text file and organize it by category (User name,User ID, User password) I am having troubles allocating an array and storing the information correctly. Since I have different data types (int, string) do I need to create a...

  • Coding in c++

    I keep getting errors and i am so confused can someone please help and show the input and output ive tried so many times cant seem to get it. main.cpp #include <iostream> #include <vector> #include <string> #include "functions.h" int main() {    char option;    vector<movie> movies;     while (true)     {         printMenu();         cin >> option;         cin.ignore();         switch (option)         {            case 'A':            {                string nm;                int year;                string genre;                cout << "Movie Name: ";                getline(cin, nm);                cout << "Year: ";                cin >> year;                cout << "Genre: ";                cin >> genre;                               //call you addMovie() here                addMovie(nm, year, genre, &movies);                cout << "Added " << nm << " to the catalog" << endl;                break;            }            case 'R':            {                   string mn;                cout << "Movie Name:";                getline(cin, mn);                bool found;                //call you removeMovie() here                found = removeMovie(mn, &movies);                if (found == false)                    cout << "Cannot find " << mn << endl;                else                    cout << "Removed " << mn << " from catalog" << endl;                break;            }            case 'O':            {                string mn;                cout << "Movie Name: ";                getline(cin, mn);                cout << endl;                //call you movieInfo function here                movieInfo(mn, movies);                break;            }            case 'C':            {                cout << "There are " << movies.size() << " movies in the catalog" << endl;                 // Call the printCatalog function here                 printCatalog(movies);                break;            }            case 'F':            {                string inputFile;                bool isOpen;                cin >> inputFile;                cout << "Reading catalog info from " << inputFile << endl;                //call you readFromFile() in here                isOpen = readFile(inputFile, &movies);                if (isOpen == false)                    cout << "File not found" << endl;...

  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

  • I am trying to run this program in Visual Studio 2017. I keep getting this build...

    I am trying to run this program in Visual Studio 2017. I keep getting this build error: error MSB8036: The Windows SDK version 8.1 was not found. Install the required version of Windows SDK or change the SDK version in the project property pages or by right-clicking the solution and selecting "Retarget solution". 1>Done building project "ConsoleApplication2.vcxproj" -- FAILED. #include <iostream> #include<cstdlib> #include<fstream> #include<string> using namespace std; void showChoices() { cout << "\nMAIN MENU" << endl; cout << "1: Addition...

  • Fix my code, if I the song or the artist name is not on the vector,...

    Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...

  • Having issues using binary search on a pointer array. 1. Write 1000 random ints to file...

    Having issues using binary search on a pointer array. 1. Write 1000 random ints to file 2. Binary Search to find if number exists in element from file. int main() { ArrayActions action;   int count; int userInput; int num = 1000; int array[num]; ofstream myFile ("/Users/chan/Desktop/LANEY_CIS27/Assignemtn2_CIS27/Assignemtn2_CIS27/File.txt");   //Set srand with time to generate unique random numbers srand((unsigned)time(0));   //Format random num up to 999 for(count = 0; count < num; count++) { array[count] = rand() % 1000; }          //Condition...

  • I need help debugging this C++ prgram. What Am i doing wrong? //******************************************************** // This program...

    I need help debugging this C++ prgram. What Am i doing wrong? //******************************************************** // This program reads two input files whose lines are //ordered by a key data field. This program should merge //these two files, writing an output file that contains //all lines from both files ordered by the same key field. // //********************************************************* #include <iostream> #include<string> #include<fstream> //prototype void mergeTwoFiles (ifstream&,ifstream&, ofstream&); using namespace std; int main() {string inFile1,inFile2,outFile; // input and output files ifstream in1; ifstream in2;...

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