473,387 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to compare two different arrays.

Good-day,

I am doing a class project in C++ and am having trouble with one of my menu items. I am trying to inplement a test for the user to try in a fraction class/ porgram. I have filled one array with the answers to the test and then am filling the second array with answers form the user. The third array is a bool array and is being filled by comparing the first two arrays and recording correct/ incorrect anwers. In turn, I am counting the correct answers. All this is done within three functions.

When the choice (S) is made from the menu - the test is displayed and the user is prompted to enter his answers. When the return key is hit - a continuous loop occurs.

I am very new to programing and to be honest not very good at it.

Help is greatly appreciated.

Here is the main program.
Apr 2 '07 #1
6 2951
Savage
1,764 Expert 1GB
Good-day,

I am doing a class project in C++ and am having trouble with one of my menu items. I am trying to inplement a test for the user to try in a fraction class/ porgram. I have filled one array with the answers to the test and then am filling the second array with answers form the user. The third array is a bool array and is being filled by comparing the first two arrays and recording correct/ incorrect anwers. In turn, I am counting the correct answers. All this is done within three functions.

When the choice (S) is made from the menu - the test is displayed and the user is prompted to enter his answers. When the return key is hit - a continuous loop occurs.

I am very new to programing and to be honest not very good at it.

Help is greatly appreciated.


Hi,there.I don't understand your problem.Can you be a little more specific?

(Are there any errors ,are you stucked,where are you stucked...)

And please don't post full code because it will be snniped by a moderator.Post only troubleshooting part

Savage
Apr 2 '07 #2
sicarie
4,677 Expert Mod 4TB
bing3121-
Please read the Posting Guidelines in the FAQ tab at the top of this page. You have not asked any sort of a question, please repost a specific question with only the relevant portions of code (but thank you for using code tags!).
Apr 2 '07 #3
Hi,there.I don't understand your problem.Can you be a little more specific?

(Are there any errors ,are you stucked,where are you stucked...)

And please don't post full code because it will be snniped by a moderator.Post only troubleshooting part

Savage
Point taken about the code.

The question I am asking, is from the functions I am displaying, am I accomplishing what I said I want to do in the initial posting. That is I am trying to fill an array from an input file and having the user fill in the other array with the answers he/she has calculated on their own. Then I am trying to compare the array with the input file answers with the arry filled with the user answers to return a score. From the code posted it is not working. I am using Visual C++ 6.0 to build. Everything compiles and the program will run fine - until the choice (S) is selected. The test questions will display and and the user is prompted for an answer. As soon as the user enters an answer a continual loop occurs.

Expand|Select|Wrap|Line Numbers
  1. void get_scored_test()
  2. {
  3.     ifstream input ("fractest.in");
  4.     string next;
  5.  
  6.     if (input.is_open())
  7.     {
  8.         while (!input.eof())
  9.         {
  10.             getline(input, next);
  11.             cout<<next<< endl;
  12.         }
  13.         input.close();
  14.     }
  15.     else
  16.         cout<<"Sorry Test is not available"<< endl;
  17. }
  18.  
  19. void fill_scored_test()
  20. {
  21.     ifstream test_input; 
  22.     int test_answers[5];
  23.     int answers[5];
  24.  
  25.     test_input.open("testanswers.in");
  26.     if(test_input.fail())
  27.     {
  28.         cout<<"Test Answers not available" <<endl;
  29.         exit(1);
  30.     }
  31.  
  32.     for(int i=0;i<5;i++)
  33.     {
  34.         test_input>>test_answers[i];
  35.     }
  36.  
  37.     test_input.close();
  38.  
  39.     for(int x = 0; x<5;++x)
  40.     {
  41.         cout<<"Enter your answers one at a time. " <<endl;
  42.         cin>>answers[x];
  43.     }
  44. }
  45.  
  46. int mark_scored_test()
  47. {
  48.     bool check[5];
  49.     int count=0;
  50.     int test_answers[5];
  51.     int answers[5];
  52.  
  53.     for(int z = 0; z<5;z++)
  54.     {
  55.         if(test_answers[z]==answers[z])
  56.         {
  57.             check[z]=true;
  58.             ++count;
  59.         }
  60.         else
  61.             check[z]=false;
  62.     }
  63.     return(count);
  64.  
  65.     cout<< "your score for the test is ==> " << count << endl;
  66. }
Apr 2 '07 #4
bing3121-
Please read the Posting Guidelines in the FAQ tab at the top of this page. You have not asked any sort of a question, please repost a specific question with only the relevant portions of code (but thank you for using code tags!).

Understood, new to this
Apr 2 '07 #5
sicarie
4,677 Expert Mod 4TB
Expand|Select|Wrap|Line Numbers
  1. void get_scored_test()
  2. {
  3.     ifstream input ("fractest.in");
  4.     string next;
  5.  
  6.     if (input.is_open())
  7.     {
  8.         while (!input.eof())
  9.         {
  10.             getline(input, next);
  11.             cout<<next<< endl;
  12.         }
  13.         input.close();
  14.     }
  15.     else
  16.         cout<<"Sorry Test is not available"<< endl;
  17. }
  18.  
  19. void fill_scored_test()
  20. {
  21.     ifstream test_input; 
  22.     int test_answers[5];
  23.     int answers[5];
  24.  
  25.     test_input.open("testanswers.in");
  26.     if(test_input.fail())
  27.     {
  28.         cout<<"Test Answers not available" <<endl;
  29.         exit(1);
  30.     }
  31.  
  32.     for(int i=0;i<5;i++)
  33.     {
  34.         test_input>>test_answers[i];
  35.     }
  36.  
  37.     test_input.close();
  38.  
  39.     for(int x = 0; x<5;++x)
  40.     {
  41.         cout<<"Enter your answers one at a time. " <<endl;
  42.         cin>>answers[x];
  43.     }
  44. }
  45.  
  46. int mark_scored_test()
  47. {
  48.     bool check[5];
  49.     int count=0;
  50.     int test_answers[5];
  51.     int answers[5];
  52.  
  53.     for(int z = 0; z<5;z++)
  54.     {
  55.         if(test_answers[z]==answers[z])
  56.         {
  57.             check[z]=true;
  58.             ++count;
  59.         }
  60.         else
  61.             check[z]=false;
  62.     }
  63.     return(count);
  64.  
  65.     cout<< "your score for the test is ==> " << count << endl;
  66. }
How are your variables declared? You have a lot of local variables that aren't returned...
Apr 2 '07 #6
How are your variables declared? You have a lot of local variables that aren't returned...
Hello,

Everything is local to the fucntion it is in. The only return is count - how the user scored on the test.

Maybe arrays are not the way to go. Do you have any suggestions?

I am using a fraction class and have been trying to modify mu usage file.

Here is a copy of the menu. You can see how the functions are called.

Expand|Select|Wrap|Line Numbers
  1. choice = toupper(get_user_command());
  2.  
  3.     do
  4.     {
  5.         switch (choice)
  6.         {
  7.             case 'T': fraction_tutorial();
  8.                       break;
  9.             case 'M': print_menu();
  10.                       break;
  11.             case '-': get_fraction(f1);
  12.                       get_fraction(f2);
  13.                       f3=f1-f2;
  14.                       cout << "\n" << f1 << "-" << f2 << " = " << f3 << endl;
  15.                       break;
  16.             case '+': get_fraction(f1);
  17.                       get_fraction(f2);
  18.                       f3=f1+f2;
  19.                       cout << "\n" << f1 << "+" << f2 << " = " << f3 << endl;
  20.                       break;
  21.             case '*': get_fraction(f1);
  22.                       get_fraction(f2);
  23.                       f3=f1*f2;
  24.                       cout << "\n" << f1 << "*" << f2 << " = " << f3 << endl;
  25.                       break;
  26.             case '/': get_fraction(f1);
  27.                       get_fraction(f2);
  28.                       f3=f1/f2;
  29.                       cout << "\n" << f1 << "/" << f2 << " = " << f3 << endl;
  30.                       break;
  31.             case 'R': get_fraction(f1);
  32.                       f1.reduce();
  33.                       cout << "The reduced form is:" << f1 << endl;    
  34.                       break;
  35.             case 'D': get_fraction(f1);
  36.                       f1.decimal_value();
  37.                       cout<<"the Decimal value is ==> " << f1 << endl; 
  38.                       break;
  39.             case 'P': get_practice_test();
  40.                       break;
  41.             case 'S': get_scored_test();
  42.                       fill_scored_test();
  43.                       mark_scored_test();
  44.                       break;
  45.             case 'Q': cout <<"Math only leads to greatness or a big headache and sometimes both" << endl;
  46.                       cout <<"If you must quit then enter W" << endl;
  47.                       break;
  48.             default: cout << "The" << choice << "is not a valid choice" << endl;
  49.         }
  50.  
  51.         choice = toupper(get_user_command());
  52.     }while(choice != 'W');
Apr 3 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Phil Powell | last post by:
/*-------------------------------------------------------------------------------------------------------------------------------- Parameters: $formField1: The name of the first array $formField2:...
8
by: Devrobcom | last post by:
Hi I have an array with 30 int and another array with 1000 int. If any of the 30 intergers can be found in the 1000 int array I shall return true. The contents of the arrays will also change over...
4
by: Jim Andersen | last post by:
Is there anything built into .NET that is good (or rather easy) at comparing ? I have some data (in an array). I make a copy of this array, and the user changes some of the data, or maybe he...
1
by: kara18 | last post by:
Hi, After splitting a string in to tokens using strtok ,how can I get the tokens in to different arrays. for example char str = "now # is the time for all # good men to come to the # aid of...
3
by: John | last post by:
I have two large arrays. Is there a rapid method to comaprs the two arrays and creat a third array of onlt thos items that the are in the first array but not the second array. I do it manually...
0
gits
by: gits | last post by:
This little article will show you how to optimize runtime performance when you need to compare two arrays (a quite common task). Have a close look at the entire article, and you will see the...
3
by: gaurav soni | last post by:
Hi, guys i am not very much familiar with c/c++ programming . i am trying to compare four ,character arrays i.e how and who are equal to each other. kindly update me ASAP . Thanku
3
by: DERosenblum | last post by:
Hi, I'm a total newbie at programming. I am looking to compare the an element of array2 with the next element of array2, and if equal, put a 1 in the same spot of array3. For example, if array2...
1
semanticnotion
by: semanticnotion | last post by:
Hello all, i have two arrays i.e $ar1=array("Mobile","shop","software","hardware"); and $arr2=arry("shop","Mobile","shop","software","shop") i want to compare the elements of arr2 to arr1 i.e...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.