Question

I need help with a c++ function. I need this function to read in an input...

I need help with a c++ function. I need this function to read in an input file containing multiple choice questions/potential answers and to store that data into a 2d array of strings. This function cannot use classes or vectors or anything "advanced". The input file will have 0-50 questions; each with 4 multiple choice responses. The array should be titled questionArray. (it's a .txt file)

update:

What is my name?

John

Bob

Lance

Mike

What is my favorite animal?

Cat

Dog

Lizard

Tiger

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

Thanks for the question, here is the code in CPP, I have commented the code so that you can folow the logic, understand and learn the concepts

Let me know for any help with any other questions.

==============================================================

#include<string>

#include<fstream>

#include<iostream>

using namespace std;

int main(){

               

                char filename[] ="D:\\questions.txt"; // input text file that needs to be read

               

                string questionArray[50][5]; // there wll be 5 columns and 50 rows 2D string array

                int index = 0;

                // each row will reprsent 1 question,

                // first column will be the question

                // 2nd to 5th columns will contain the 4 options

               

                ifstream infile(filename);

                if(infile.is_open()){

                               

                                while(getline(infile,questionArray[index][0],'\n')){ // read the first question

                                                getline(infile,questionArray[index][1],'\n'); // read the option 1 is next line

                                                getline(infile,questionArray[index][2],'\n');// read the option 2 is next line

                                                getline(infile,questionArray[index][3],'\n'); // read the option 3 is next line

                                                getline(infile,questionArray[index][4],'\n'); // read the option 4 is next line

                                               

                                                index+=1;

                                }

                               

                                infile.close();

                               

                               

                }else{

                                cout<<"Error: Unable to open/read from file: "<< filename <<endl;

                                return 1;

                }

               

                cout<<"Printing all Questions from file\n\n";

                for(int i=0; i<index;i++){

                                cout<<"Q("<<i+1<<") "<<questionArray[i][0]<<endl;

                                cout<<"[A] "<<questionArray[i][1]<<endl;

                                cout<<"[B] "<<questionArray[i][2]<<endl;

                                cout<<"[C] "<<questionArray[i][3]<<endl;

                                cout<<"[D] "<<questionArray[i][4]<<endl<<endl;

                }

               

}

==============================================================

Thanks , please do give a thumbs up !

Add a comment
Know the answer?
Add Answer to:
I need help with a c++ function. I need this function to read in an input...
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
  • Need help with output form -I need my variables and strings in my print function to...

    Need help with output form -I need my variables and strings in my print function to be separated by underscores _. Thanks. m, d, y=eval(input("Enter the date like so m, d, y: ")) files=eval(input("How many files?: ")) fName=(input("Enter your family name: ")) FROM THIS: print(fName,m,d,y,".txt") FORM OUTPUT NEEDS TO BE IN: name_month_day_year.txt

  • I need help writing these functions in C programming. Write a C function that checks if...

    I need help writing these functions in C programming. Write a C function that checks if an array is sorted or not using an iterative approach. Write a C function that checks if an array is sorted or not using a recursive approach Write a C function to reverse the elements of an array. Note you are not allowed to use an 1. additional array to do that Write a C function to find the sum of the elements of...

  • Validating Input file, So for my C++ assignment, my professor says we need to validate the...

    Validating Input file, So for my C++ assignment, my professor says we need to validate the Input File, Which means, if the line is missing a comma, one of the 3 information, or the string is a white line, it will ignore it. If the line has a white line, the program will correct it and will read in the line. I will store these into an Array, and display them in First name, Last name, and Number of Votes...

  • This should be in PHP, please For Project 1, you need to create a Web form...

    This should be in PHP, please For Project 1, you need to create a Web form to present a ten (5) Multiple Choice quiz about the Chinese zodiac signs. Indicate that all of the questions must be answered for the quiz to be graded. You can use radio buttons or drop-down boxes Multiple Choice answers. Requirements for Project 1: Provide fields for visitors to enter their name and answers to the questions Notify the visitor that the fields are required...

  • I need help with this program. Im trying to use C++ programming language. I want to...

    I need help with this program. Im trying to use C++ programming language. I want to include my ifstream SimpleCal6.txt and my outFile. CS 317 Calculator Program Write a program to input three values, two floats and an operator print out what were read and the resulting evaluation. The operators to handle are +, -, *, / and ^. The floats are one or more digits in length. The last input is sentinel value 0 + 0; Read a float,...

  • I am currently facing a problem with my python program which i have pasted below where i have to design and implement python classes and record zoo database and takes user input as query. the error i...

    I am currently facing a problem with my python program which i have pasted below where i have to design and implement python classes and record zoo database and takes user input as query. the error i am recieving says that in line 61 list index is out of range. kindly someone help me with it as soon as possible. Below is the program kindly check and correct it. Thanks! class Animal: def __init__(self, name, types, species, mass): self.name=name self.type=types...

  • I need only one  C++ function . It's C++ don't write any other language. Hello I need...

    I need only one  C++ function . It's C++ don't write any other language. Hello I need unzip function here is my zip function below. So I need the opposite function unzip. Instructions: The next tools you will build come in a pair, because one (zip) is a file compression tool, and the other (unzip) is a file decompression tool. The type of compression used here is a simple form of compression called run-length encoding (RLE). RLE is quite simple: when...

  • I need python help .. I need to know if a user were to input 3...

    I need python help .. I need to know if a user were to input 3 commands to a program and run it such as specify: Usage: ./filemaker INPUTCOMMANDFILE OUTPUTFILE RECORDCOUNT (./filemaker is the program)( (INPUTCOOMANDFUILE= a text file that contains key words that need to be searched through then decided what to do ) (RECORDCOUNT= number) Given an input file that contains the following: That specifies the text, then the output, and the number of times to be repeated...

  • I need help with this code, I'm stuck on it, please remember step 4, I'm very...

    I need help with this code, I'm stuck on it, please remember step 4, I'm very much stuck on that part. It says something about putting how many times it appears Assignment #1: Sorting with Binary Search Tree Through this programming assignment, the students will learn to do the following: Know how to process command line arguments. 1 Perform basic file I/O. 2. Use structs, pointers, and strings. Use dynamic memory. 3. 4. This assignment asks you to sort the...

  • Program in C Student Data using Structs In this assignment you will create a program (using...

    Program in C Student Data using Structs In this assignment you will create a program (using multiple .c files) to solve the following problem: You have been hired by the university to create a program that will read in input from a .txt file. This input will contain a student name, student id #, their age and their current gpa. You should use the following struct for your student data: struct Student{                 char name[50];                 int id;                 float...

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