printf("%11.11s %2d\n", student_id[i],correct_count);
}
cout << endl << endl << endl << "Number of students taking exam : " << student_count << endl;
printf("\nquestion A B C D E\n");
for(int k = 0; k < 20; k++) {
printf(" %2d ",1+k);
for(int l=0;l < 5; l++)
printf(" %2d%c ", question[k][l], (l == (correct_keys[k] - 'A') ? '*' : ' '));
printf("\n");
}
23 Replies - 1921 Views - Last Post: 23 November 2009 - 10:03 PM
#1
printf to cout
Posted 19 November 2009 - 11:11 PM
Replies To: printf to cout
#2
Re: printf to cout
Posted 19 November 2009 - 11:24 PM
Unless you just want a code handout. Isn't that what you're really asking? Here's the code, do what I want with it and give me the answer.
#3
Re: printf to cout
Posted 19 November 2009 - 11:43 PM
Oler1s, on 19 Nov, 2009 - 10:24 PM, said:
Unless you just want a code handout. Isn't that what you're really asking? Here's the code, do what I want with it and give me the answer.
I wrote a code and this is the last part of the code and yes I am just asking for the cout code if it is possible or you can help me change it to cout by myselff
#4
Re: printf to cout
Posted 20 November 2009 - 12:16 AM
thebeast91, on 19 Nov, 2009 - 10:43 PM, said:
Oler1s, on 19 Nov, 2009 - 10:24 PM, said:
Unless you just want a code handout. Isn't that what you're really asking? Here's the code, do what I want with it and give me the answer.
I wrote a code and this is the last part of the code and yes I am just asking for the cout code if it is possible or you can help me change it to cout by myselff
Him aside, here's the secret. Cout is Printf for the most part. Really, in your printf statement, you use %d and then tell it what %d is. In Cout you don't need to tell it to expect an integer, you can just print it. So if you had this.
printf("%d",x);
//cout code for above
cout << x;
I think you can figure the rest out.
Also, you CAN use the ternary operator (IE: cond ? return x: return y) to do output using Cout, it will be evaluated before it decides what to put out. In fact, here's an example.
int x = 1; cout << (x == 1?"Yes":"No") << endl;
#5
Re: printf to cout
Posted 20 November 2009 - 12:37 AM
ccubed, on 19 Nov, 2009 - 11:16 PM, said:
thebeast91, on 19 Nov, 2009 - 10:43 PM, said:
Oler1s, on 19 Nov, 2009 - 10:24 PM, said:
Unless you just want a code handout. Isn't that what you're really asking? Here's the code, do what I want with it and give me the answer.
I wrote a code and this is the last part of the code and yes I am just asking for the cout code if it is possible or you can help me change it to cout by myselff
Him aside, here's the secret. Cout is Printf for the most part. Really, in your printf statement, you use %d and then tell it what %d is. In Cout you don't need to tell it to expect an integer, you can just print it. So if you had this.
printf("%d",x);
//cout code for above
cout << x;
I think you can figure the rest out.
Also, you CAN use the ternary operator (IE: cond ? return x: return y) to do output using Cout, it will be evaluated before it decides what to put out. In fact, here's an example.
int x = 1; cout << (x == 1?"Yes":"No") << endl;
thank you that helped a lot thats what i was asking .I am having some problems with the rest of my code.I dont want you to tell me the code I just want some help without giving away the code.
this is what i am working on
A class of students takes a 20 question multiple-choice exam;each question has 5 choices(a,b,c,d,e)only one of them are correct.In "tests.dat"Each record of which consists of a student id, followed by a blank, followed by students 20 responses. In "answers.dat" which consists of a single record, namely the correct answers to the exam.Then It needs to produce a grade summary report like this.If the answer is correct it should put a "*" by the correct answer
student-id number correct
1231231212312 12
1233424325435 25
.... ...
question A B C D E
1 5 1 13* 3 1
2 4 7* 5 12 7
.
.
...
and this is wat i got so far
#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int question[20][5];
int main()
{
ifstream fanswers, fcorrect_keys;
char correct_keys[21];
char student_id[100][12], answers[100][21];
char id_buf[12], answers_buf[21];
int student_count = 0;
fcorrect_keys.open("Answers.dat");
while(!fcorrect_keys.eof()) {
fcorrect_keys >> answers_buf;
strcpy(correct_keys, answers_buf);
}
// cout << correct_keys << endl;
fcorrect_keys.close();
fanswers.open("Tests.dat");
while(!fanswers.eof()) {
fanswers >> id_buf >> answers_buf;
strcpy(student_id[student_count], id_buf);
strcpy(answers[student_count],answers_buf);
student_count++;
}
// for(int i=0; i < student_count;i++)
// cout << student_id[i] << " " << answers[i] << endl;
fanswers.close();// close the file
cout << " student_id number_correct" << endl << endl;
for(int i = 0; i < student_count; i++) {
int correct_count = 0;
for(int j = 0; j < 20; j++) {
if (answers [i][j] == correct_keys[j])
correct_count++;
question[j][answers[i][j] - 'A']++;
}
printf("%11.11s %2d\n", student_id[i],correct_count);
}
cout << endl << endl << endl << "Number of students taking exam : " << student_count << endl;
printf("\nquestion A B C D E\n");
for(int k = 0; k < 20; k++) {
printf(" %2d ",1+k);
for(int l=0;l < 5; l++)
printf(" %2d%c ", question[k][l], (l == (correct_keys[k] - 'A') ? '*' : ' '));
printf("\n");
}
return 0;
}
*Mod Edit: fixed quote
#6
Re: printf to cout
Posted 20 November 2009 - 12:41 AM
Clarification: Students would have a vector that stores a student array that contains 20 answers. So one array for each student.
This post has been edited by ccubed: 20 November 2009 - 12:42 AM
#7
Re: printf to cout
Posted 20 November 2009 - 12:43 AM
ccubed, on 19 Nov, 2009 - 11:41 PM, said:
Clarification: Students would have a vector that stores a student array that contains 20 answers. So one array for each student.
by just looking at my code can you tell me what i did wrong bc when i look at it,it seems pretty right
#8
Re: printf to cout
Posted 20 November 2009 - 12:50 AM
#9
Re: printf to cout
Posted 20 November 2009 - 12:55 AM
ccubed, on 19 Nov, 2009 - 11:50 PM, said:
OK..When I run the code below, It just prints out student-id, number correct table then it gives an error and says the program stopped working.If i change "strcpy"s to "strcpy_s" it prints out both the tables but in the student-id table the number corrects are all "0"s and in the question A B C D E table everything is fine except it never prints out"*" after the right answer and in D column there are 1 more then the number there should be. I hope I made it clear.
#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int question[20][5];
int main()
{
ifstream fanswers, fcorrect_keys;
char correct_keys[21];
char student_id[100][12], answers[100][21];
char id_buf[12], answers_buf[21];
int student_count = 0;
fcorrect_keys.open("Answers.dat");
while(!fcorrect_keys.eof()) {
fcorrect_keys >> answers_buf;
strcpy(correct_keys, answers_buf);
}
// cout << correct_keys << endl;
fcorrect_keys.close();
fanswers.open("Tests.dat");
while(!fanswers.eof()) {
fanswers >> id_buf >> answers_buf;
strcpy(student_id[student_count], id_buf);
strcpy(answers[student_count],answers_buf);
student_count++;
}
// for(int i=0; i < student_count;i++)
// cout << student_id[i] << " " << answers[i] << endl;
fanswers.close();// close the file
cout << " student_id number_correct" << endl << endl;
for(int i = 0; i < student_count; i++) {
int correct_count = 0;
for(int j = 0; j < 20; j++) {
if (answers [i][j] == correct_keys[j])
correct_count++;
question[j][answers[i][j] - 'A']++;
}
printf("%11.11s %2d\n", student_id[i],correct_count);
}
cout << endl << endl << endl << "Number of students taking exam : " << student_count << endl;
printf("\nquestion A B C D E\n");
for(int k = 0; k < 20; k++) {
printf(" %2d ",1+k);
for(int l=0;l < 5; l++)
printf(" %2d%c ", question[k][l], (l == (correct_keys[k] - 'A') ? '*' : ' '));
printf("\n");
}
return 0;
}
#10
Re: printf to cout
Posted 20 November 2009 - 01:09 AM
question[j][answers[i][j] - 'A']++
What are you trying to accomplish by subtracting an 'A'? Even if you do int('A') I can't see it giving any useful purpose here.
Also..Question is a 20x5 array of int and answers is a 100x21 of char. So why are we trying to access an array by subtracting a char from answers by the char 'A'?
Also, question seems dubious. I'm not entirely sure what you're using it for. Though I suppose it might be for holding the student's answers. In which case, you're assuming there's only 5 students in question but 100 in answers? There's also an extra question in answers. You should be consistent with the numbers. 100 students across the board and 20 or 21 answers.
#11
Re: printf to cout
Posted 20 November 2009 - 01:46 AM
ccubed, on 20 Nov, 2009 - 12:09 AM, said:
question[j][answers[i][j] - 'A']++
What are you trying to accomplish by subtracting an 'A'? Even if you do int('A') I can't see it giving any useful purpose here.
Also..Question is a 20x5 array of int and answers is a 100x21 of char. So why are we trying to access an array by subtracting a char from answers by the char 'A'?
Also, question seems dubious. I'm not entirely sure what you're using it for. Though I suppose it might be for holding the student's answers. In which case, you're assuming there's only 5 students in question but 100 in answers? There's also an extra question in answers. You should be consistent with the numbers. 100 students across the board and 20 or 21 answers.
What I was trying to there is I was trying to count answer A. So what you are telling me is I should change all of them to char.There 23 students and their id s are 11 digits.Am i getting closer or did I make the code worse?
#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
char question[20][5];
int main()
{
ifstream fanswers, fcorrect_keys;
char correct_keys[21];
char student_id[12], answers[5][20];
char id_buf[12], answers_buf[20];
char student_count = 0;
fcorrect_keys.open("Answers.dat");
while(!fcorrect_keys.eof()) {
fcorrect_keys >> answers_buf;
strcpy(correct_keys, answers_buf);
}
// cout << correct_keys << endl;
fcorrect_keys.close();
fanswers.open("Tests.dat");
while(!fanswers.eof()) {
fanswers >> id_buf >> answers_buf;
strcpy(student_id[student_count], id_buf);
strcpy(answers[student_count],answers_buf);
student_count++;
}
// for(int i=0; i < student_count;i++)
// cout << student_id[i] << " " << answers[i] << endl;
fanswers.close();// close the file
cout << " student_id number_correct" << endl << endl;
for(int i = 0; i < student_count; i++) {
int correct_count = 0;
for(int j = 0; j < 20; j++) {
if (answers [i][j] == correct_keys[j])
correct_count++;
question[j][answers[i][j] - 'A']++;
}
printf("%11.11s %2d\n", student_id[i],correct_count);
}
cout << endl << endl << endl << "Number of students taking exam : " << student_count << endl;
printf("\nquestion A B C D E\n");
for(int k = 0; k < 20; k++) {
printf(" %2d ",1+k);
for(int l=0;l < 5; l++)
printf(" %2d%c ", question[k][l], (l == (correct_keys[k] - 'A') ? '*' : ' '));
printf("\n");
}
return 0;
}
#12
Re: printf to cout
Posted 20 November 2009 - 09:54 AM
As for the 'A', assuming that you mean to count the number of A's answered, you don't really need to subtract A. You should just [Student][1-5]. No reason to dynamically code the 1-5 bit since it won't change, ever.
#13
Re: printf to cout
Posted 21 November 2009 - 10:57 PM
ccubed, on 20 Nov, 2009 - 08:54 AM, said:
As for the 'A', assuming that you mean to count the number of A's answered, you don't really need to subtract A. You should just [Student][1-5]. No reason to dynamically code the 1-5 bit since it won't change, ever.
I wrote the code all over again. It says that I am doing something wrong when I am typing the while loop line. THis is the code
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
int main()
{
ifstream finans,fintests;
char key[21],id[12],responses[21];
int count[20][5];
int correct_count;
fintests.open("Tests.dat");
fintests>>id[12]>>responses[21];
finans.open("Answers.dat");
finans>>key[21];
while(!fin.eof())
{
for(int i = 0; i < student_count; i++)
{
int correct_count = 0;
for(int j = 0; j < 20; j++)
if (answers [i][j] == correct_keys[j])
correct_count++;
question[j][answers[i][j] - 'A']++;
printf("%11.11s %2d\n", student_id[i],correct_count);
}
cout << " student_id number_correct" << endl << endl;
cout<<id[21]<<"\t"<<correct_count<<endl;
cout << endl << endl << endl << "Number of students taking exam : " << student_count << endl;
printf("\nquestion A B C D E\n");
for(int k = 0; k < 20; k++) {
printf(" %2d ",1+k);
for(int l=0;l < 5; l++)
printf(" %2d%c ", question[k][l], (l == (correct_keys[k] - 'A') ? '*' : ' '));
printf("\n");
}
return 0;
}
#14
Re: printf to cout
Posted 22 November 2009 - 12:43 AM
That's usually the first thing you should check. It's simple to do, just go down the line and count. Every { = +1 and every } = -1. If you don't have 0 at the end something is missing.
This post has been edited by ccubed: 22 November 2009 - 12:43 AM
#15
Re: printf to cout
Posted 22 November 2009 - 09:47 PM
ccubed, on 21 Nov, 2009 - 11:43 PM, said:
That's usually the first thing you should check. It's simple to do, just go down the line and count. Every { = +1 and every } = -1. If you don't have 0 at the end something is missing.
hmm.ok well I really dont know how to write the while loop..I am doing that part wrong
|
|

New Topic/Question
Reply




MultiQuote




|