Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8687737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:11:20+00:00 2026-06-12T23:11:20+00:00

I’ve checked all my strings and arrays. Nothing looks wrong to me. My include

  • 0

I’ve checked all my strings and arrays. Nothing looks wrong to me. My include statements may be the problem. Perhaps I am missing one. I’m am a novice programmer. So I am clueless as to why this code will not work. It looks right and should absolutely work.

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

int main()
{
        int i;
        char response;
        char answers[20];
        char uresponse[20];
        int score=0;


        srand(time(0));
        int test_num = rand() % 2+1;
//        cout << test_num << endl;
        if (test_num == 1)
        {
                string line;
                ifstream myfile ("form1.txt");
                getline (myfile,line);
//                cout<< line<<endl;
                for (int i = 0; i <= 19; i++)
                {
                        answers[i] = line[i*2];
//                        cout<<answers[i]<<endl;
                }
                getline (myfile,line);

                  if ( myfile.is_open())
                  {
                          int x = 0;
                        while ( myfile.good() )
                        {
                          char answers[20];
                              string line;
                                getline (myfile,line);
//                                cout<< line << endl;
                                if( line == "")
                                {
                                        cout << "Enter answer: ";
                                        cin >> response;
                                        if(response == 'a', 'A' || response == 'b', 'B' || response == 'c', 'C' || response == 'd', 'D')
                                        {
                                                response = response;
                                        }
                                        else
                                        {
                                                cout << "ERROR. You must enter A, B, C, or D. Please re-enter: ";
                                                cin >> response;
                                        }
                                        response = toupper(response);
                                        uresponse[x] = response;
                                        x++;
                                }
                        }
//                      cout << uresponse <<endl;
//                      system("pause");
                 }
                  else
  {
          myfile.close();
  }
        }
        else
                {
                string line;
                ifstream myfile ("form2.txt");
                getline (myfile,line);
 //               cout<< line<<endl;
                for (int i = 0; i <= 19; i++)
                {
                        answers[i] = line[i*2];
//                        cout<<answers[i]<<endl;
                }
                getline (myfile,line);

                  if ( myfile.is_open())
                  {
                        int x = 0;
                        while ( myfile.good() )
                        {
                          char answers[20];
                              string line;
                                getline (myfile,line);
 //                               cout << line << endl;
                                if( line == "")
                                {
                                        cout << "Enter answer:";
                                        cin >> response;
                                        if(response == 'a', 'A' || response == 'b', 'B' || response == 'c', 'C' || response == 'd', 'D')
                                        {
                                                response = response;
                                        }
                                        else
                                        {
                                                cout << "ERROR:You must enter A, B, C, or D. Please re-enter:";
                                                cin >> response;
                                        }
                                        response = toupper(response);
                                        uresponse[x] = response;
                                        x++;
                                }
                        }
//                      cout<<uresponse<<endl;
//                    system("pause");
                 }
                  else
  {
          myfile.close();
  }
        }

        for (int j=0; j<20; j++)
        {
                if (answers[j] == uresponse[j])
                        score += 1;
        }

        if(score >15)
        {
                cout << "CONGRATULATIONS! You Passed with a score of ";
                cout << score << endl;
                system("pause");
        }
        else
        {
                cout<< "You Failed with a score of ";
                cout << score << endl;
                system("pause");
        }
        system("pause");
        return 0;
        }`enter code here`
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:11:21+00:00Added an answer on June 12, 2026 at 11:11 pm

    This could be the culprit:

    answers[i] = line[i*2];
    

    Are you certain i*2 is not >= line.size()?

    Btw, this code looks dodgy:

    if(response == 'a', 'A' || response == 'b', 'B'
     || response == 'c', 'C' || response == 'd', 'D')
    {
         response = response   
    }
    

    Perhaps you want to use a switch and initializing response to itself.. why?

    EDIT:
    And your while(myfile.good())
    That probably executes 100 times and you keep incrementing x
    It’s hard to say in your code.. you have a lot of “dodgy” lines 😛

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I've tracked down a weird MySQL problem to the two different ways I was
I have a text area in my form which accepts all possible characters from
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.