c++ program dealing with arrays

c++ program dealing with arrays

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 5866 Views - Last Post: 09 December 2008 - 07:34 AM Rate Topic: -----

#1 tinathila   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-December 08

c++ program dealing with arrays

Post icon  Posted 02 December 2008 - 07:37 PM

11. Driver’s license exam
The local driver’s license office has asked you to write a program that grades the written portion of the driver’s license exam. The exam has 10 multiple choice questions. Here are the answers:

1.B 6.A
2.D 7.B
3.A 8.A
4.A 9.C
5.C 10.D

Your program should store the correct answers above in an array. It should ask the user to enter the student’s answers for each of the 10 questions, and the answers should be stored in another array. After the student’s answers have been entered the program should display a message indicating whether the student passed or failed the exam (7 correct is necessary for passing) It should then display the total number of correctly answered questions and the total number of incorrectly answered questions.

Input validation: only accept the letters A,B,C or D as input

Is This A Good Question/Topic? 0
  • +

Replies To: c++ program dealing with arrays

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: c++ program dealing with arrays

Posted 02 December 2008 - 07:56 PM

And? What have you done? We don't do other people's homework; we help them with whatever specific problems they may have encountered in doing their own work.

This post has been edited by JackOfAllTrades: 02 December 2008 - 07:56 PM

Was This Post Helpful? 1
  • +
  • -

#3 tinathila   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-December 08

Re: c++ program dealing with arrays

Posted 02 December 2008 - 08:31 PM

View PostJackOfAllTrades, on 2 Dec, 2008 - 06:56 PM, said:

And? What have you done? We don't do other people's homework; we help them with whatever specific problems they may have encountered in doing their own work.


is this how i'm suppose to start it
#include <iostream>

#include <iomanip> // For setprecision()

#include <string>

using namespace std;


int main()

{


int num1;

int num2;

int num3;

int num4;

int num5;



cout << fixed << showpoint // Set up floating-pt.

<< setprecision(2); // output format


cout << "Enter the num1" << endl; // Prompt for the first number
cin >> num1; // Input for the 1st number


cout << "Enter the num2" // Prompt for the 2nd number

<< endl;
cout << " ";
cin >> num2; // Input for the 2nd number


cout << "Enter the num3" // Prompt for the 3rd number

<< endl;
cout << "  ";
cin >> num3; // Input for the score for the 3rd number


cout << "Enter the num4" // Prompt for the 4th number

<< endl;
cout << "   ";
cin >> num4; // Input for the 4th number


cout << "Enter the num5" // Prompt for 5th number

<< endl;
cout << "	";
cin >> num5; // Input for the 5th number




cout << "num 1" << num1 << endl << endl

<< "num2 " << num2 << endl << endl

<< "num3 " << num3 << endl << endl

<< "num4 " << num4 << endl << endl
<< "num5 " << num5 << endl << endl;


return 0;

}


Mod edit - Please :code:
Was This Post Helpful? 0
  • +
  • -

#4 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: c++ program dealing with arrays

Posted 02 December 2008 - 09:02 PM

Please post code between the code tags
:code:
Was This Post Helpful? 1
  • +
  • -

#5 tinathila   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-December 08

Re: c++ program dealing with arrays

Posted 02 December 2008 - 09:08 PM

View Postjanotte, on 2 Dec, 2008 - 08:02 PM, said:

Please post code between the code tags
:code:



#include <iostream>

#include <iomanip> // For setprecision()

#include <string>

using namespace std;


int main()

{


int num1;

int num2;

int num3;

int num4;

int num5;



cout << fixed << showpoint // Set up floating-pt.

<< setprecision(2); // output format


cout << "Enter the num1" << endl; // Prompt for the first number
cin >> num1; // Input for the 1st number


cout << "Enter the num2" // Prompt for the 2nd number

<< endl;
cout << " ";
cin >> num2; // Input for the 2nd number


cout << "Enter the num3" // Prompt for the 3rd number

<< endl;
cout << " ";
cin >> num3; // Input for the score for the 3rd number


cout << "Enter the num4" // Prompt for the 4th number

<< endl;
cout << " ";
cin >> num4; // Input for the 4th number


cout << "Enter the num5" // Prompt for 5th number

<< endl;
cout << " ";
cin >> num5; // Input for the 5th number




cout << "num 1" << num1 << endl << endl

<< "num2 " << num2 << endl << endl

<< "num3 " << num3 << endl << endl

<< "num4 " << num4 << endl << endl
<< "num5 " << num5 << endl << endl;


return 0;

}


Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: c++ program dealing with arrays

Posted 03 December 2008 - 05:34 AM

Quote

Your program should store the correct answers above in an array


Where's the array? Look, here's a tutorial on arrays.
Was This Post Helpful? 0
  • +
  • -

#7 tinathila   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-December 08

Re: c++ program dealing with arrays

Posted 06 December 2008 - 08:33 PM

View PostJackOfAllTrades, on 3 Dec, 2008 - 04:34 AM, said:

Quote

Your program should store the correct answers above in an array


Where's the array? Look, here's a tutorial on arrays.


here is what i have so far
#include <iostream>



using namespace std;

//prototypes
char StudentAns(int );

//global variable
char choice;

int main()
{
	char key[10] = { 'B','D', 'A','A', 'C','A', 'B', 'A', 'C', 'D'};

	int i; 
	char answer [10]; 

	for(i = 0; i < 10; i++)
	{
		answer[i] = StudentAns(i);		
	}


int pos = 0;   
	if (!strcmp(choice, key[pos])) 
	  cout << "This  question is answered incorrectly."<<endl;
	else 
	{ 
			 choice =0;  // initialize counter

		 while (choice <=20)
		  {
		cout << "you have answered " << choice++ << "correctly.\n"; //no idea what you're trying to do here; this will always read 'You have answered 1 correctly', because choice never increments.
		  }
	}

	if (choice++ >= 15)
	{
		cout << "you have passed the Driver's License Exam.\n";
		cout << "Congratuations!\n";
	}
	else
	{
		cout << "You have failed the Driver's License Exam.\n";
		cout << "Please try again next time.\n";
	}

}

char StudentAns(int i)
{
	
	char choice;
		
/*	cout << "Please enter your choice for Question " << i+1 << endl;
	cin >> choice;
	choice = toupper(choice); */ //this can be condensed in your loop.
		
	while(!(choice =='A'||choice=='B'||choice=='C'||choice=='D'))
	{
		cout << "For Question: "<<i+1<<"\n Answer with A, B, C, or D.\n";
		cin >> choice;
				choice = toupper(choice);
	}

 return choice;
}



Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: c++ program dealing with arrays

Posted 06 December 2008 - 10:13 PM

Well, you started off well, but it went off the rails after you got the user's answers.

Once you've got the student's answers in the array, you do the same thing you did when you got the answers. Go through the answers array one element at a time, comparing the student's answer at each element with the answer key at the same element index. Also, you should not use strcmp in this case, but simply compare the chars straight up.

If answer[i] == key[i], they got question i + 1 correct.
Was This Post Helpful? 1
  • +
  • -

#9 tinathila   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-December 08

Re: c++ program dealing with arrays

Posted 08 December 2008 - 01:32 AM

View PostJackOfAllTrades, on 6 Dec, 2008 - 09:13 PM, said:

Well, you started off well, but it went off the rails after you got the user's answers.

Once you've got the student's answers in the array, you do the same thing you did when you got the answers. Go through the answers array one element at a time, comparing the student's answer at each element with the answer key at the same element index. Also, you should not use strcmp in this case, but simply compare the chars straight up.

If answer[i] == key[i], they got question i + 1 correct.


for some reason i changed it but its not working. it says there is a warning and i cannot figure it out.
#include <iostream>
 
using namespace std;

//prototypes
char StudentAns(int );

//global variable
char choice;

int main()
{
char key[10] = { 'B','D', 'A','A', 'C','A', 'B', 'A', 'C', 'D'};

int i;
char answer [10];

for(i = 0; i < 10; i++)
{
answer[i] = StudentAns(i);
}


int pos = 0;   
if (choice key[pos]) 
   cout << "This  question is answered incorrectly."<<endl;
else
{
			 choice =0;  // initialize counter

	 while (choice <=20)
	  {
cout << "you have answered " << choice++ << "correctly.\n"; //no idea what you're trying to do here; this will always read 'You have answered 1 correctly', because choice never increments.
	  }
}

if (choice++ >= 15)
{
cout << "you have passed the Driver's License Exam.\n";
cout << "Congratuations!\n";
}
else
{
cout << "You have failed the Driver's License Exam.\n";
cout << "Please try again next time.\n";
}

}

char StudentAns(int i)
{

char choice;

/* cout << "Please enter your choice for Question " << i+1 << endl;
cin >> choice;
choice = toupper(choice); */ //this can be condensed in your loop.

while(!(choice =='A'||choice=='B'||choice=='C'||choice=='D'))
{
cout << "For Question: "<<i+1<<"\n Answer with A, B, C, or D.\n";
cin >> choice;
				choice = toupper(choice);
}

 return choice;
}


Was This Post Helpful? 0
  • +
  • -

#10 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: c++ program dealing with arrays

Posted 08 December 2008 - 04:30 AM

View Posttinathila, on 8 Dec, 2008 - 12:32 AM, said:

for some reason i changed it but its not working. it says there is a warning and i cannot figure it out.


Please post warnings and errors that you want help with.
"there is a warning" is not enough to let us help you.
Tell us what the problem is, don't make people go searching or they are likely to just pass on to the next posting.

Here's the compiler complaining about your code
dic.cpp: In function ‘int main()’:
dic.cpp:25: error: expected `)' before ‘key’
dic.cpp:13: warning: unused variable ‘key’
dic.cpp:24: warning: unused variable ‘pos’

This is line 25
if (choice key[pos])



Do you think there is something missing between "choice" and "key[pos]"?
Was This Post Helpful? 1
  • +
  • -

#11 tinathila   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-December 08

Re: c++ program dealing with arrays

Posted 08 December 2008 - 01:50 PM

i had put a comma in between there. i think i forgot. thanks
Was This Post Helpful? 0
  • +
  • -

#12 tinathila   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-December 08

Re: c++ program dealing with arrays

Posted 08 December 2008 - 02:14 PM

for some reason it still does not want to compile. it keeps saying there is one warning
Was This Post Helpful? 0
  • +
  • -

#13 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: c++ program dealing with arrays

Posted 08 December 2008 - 03:44 PM

Perhaps a " == " was what you were thinking of for that spot?
Was This Post Helpful? 1
  • +
  • -

#14 reaper7861   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 17-November 08

Re: c++ program dealing with arrays

Posted 08 December 2008 - 03:50 PM

View Posttinathila, on 8 Dec, 2008 - 01:14 PM, said:

for some reason it still does not want to compile. it keeps saying there is one warning

Post the error that you are getting with the code it is complaining about.
My apologies Janotte didn't see your post.

This post has been edited by reaper7861: 08 December 2008 - 03:52 PM

Was This Post Helpful? 0
  • +
  • -

#15 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: c++ program dealing with arrays

Posted 08 December 2008 - 05:08 PM

View Postreaper7861, on 8 Dec, 2008 - 02:50 PM, said:

My apologies Janotte didn't see your post.


No worries at all.

My suggestion is just a 'not deeply thought through' guess.

Your idea (reposting the current code) is the 'real' answer (especially if my guess is a bad one).
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2