Thread: Vector Outputs Alien Language

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    26

    Question Vector Outputs Alien Language

    Program:
    I have 2 arrays: 1 for the correct answers to a quiz, 1 for the user. I then have a vector to hold the incorrect answers.


    It keeps outputting what looks like alt characters, and I have no idea why.


    Here is the code:


    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    
    int main()
    {
        const char a1[]={'a','d','b','b','c','b','a','b','c','d','a','c','d','b','d','c','c','a','d','b'};
        char a2[20];
        int i=0;
        int incorrect=0;
        vector<char> incorrectQuestions;
    
    
        cout<<"Drivers License Exam:\n";
        for(i=0;i<20;i++)
        {
            cout<<"Please enter the answer to question #"<<i+1<<"\n";
            cin>>a2[i];
            while(a2[i]!='a'&&a2[i]!='b'&&a2[i]!='c'&&a2[i]!='d'
                )
            {
                cout<<"Error: Please enter either a, b, c, or d:\n";
                cin>> a2[i];
            }
            if(a1[i]!=a2[i])
            {
                incorrect++;
                incorrectQuestions.push_back(i);
            }
        }
    
    
        if(incorrect>5)
        {
            cout<<"Sorry.. You failed the test.\n";
            cout<<"The number of correct answers are: "<<(20-incorrect)<<"\n";
            cout<<"The number of incorrect answers are: "<<incorrect<<"\n";
            cout<<"The incorrect questions are:\n";
            int numValues=incorrectQuestions.size();
            for(i=0;i<numValues;i++)
            {
                cout<<incorrectQuestions[i]<<"\n";
            }
        }
        else
        {
            cout<<"You passed the test!.\n";
            cout<<"The number of correct answers are: "<<(20-incorrect)<<"\n";
            cout<<"The number of incorrect answers are: "<<incorrect<<"\n";
            cout<<"The incorrect questions are:\n";
            int numValues=incorrectQuestions.size();
            for(i=0;i<numValues;i++)
            {
                cout<<incorrectQuestions[i]<<"\n";
            }
        }
        system("Pause");
        return 0;
    }

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    It just print what YOU saved in the vector:


    Code:
    if(a1[i]!=a2[i])       {
               incorrect++;
               incorrectQuestions.push_back(i);
           }
       }
    I thought you wanted to have

    Code:
     if(a1[i]!=a2[i])       {
               incorrect++;
               incorrectQuestions.push_back(a2[i]);
           }
       }
    Last edited by Aslaville; 10-19-2014 at 01:38 PM.

  3. #3
    Registered User
    Join Date
    Aug 2014
    Posts
    26
    Quote Originally Posted by Aslaville View Post
    It just print what YOU saved in the vector:


    Code:
    if(a1[i]!=a2[i])       {
               incorrect++;
               incorrectQuestions.push_back(i);
           }
       }
    I thought you wanted to have

    Code:
     if(a1[i]!=a2[i])       {
               incorrect++;
               incorrectQuestions.push_back(a2[i]);
           }
       }
    Yeah it is supposed to keep the question number(s) of the incorrect, not the contents. I figured it out though. I declared the vector as a char while i is an int.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alien swarm
    By VirtualAce in forum General Discussions
    Replies: 6
    Last Post: 08-11-2010, 03:13 PM
  2. int vector outputs wrong numbers
    By Vandrian in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2008, 12:15 PM
  3. Alien Plants Discovered: Only In 2003
    By KingZoolerius66 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 12-23-2003, 01:32 AM
  4. I'm an alien!!!
    By Leeman_s in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 01-10-2003, 05:00 PM
  5. Alien Water(MUST SEE)
    By Ruflano in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 04-02-2002, 12:20 AM