printf to cout

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 2293 Views - Last Post: 23 November 2009 - 10:03 PM Rate Topic: -----

#1 thebeast91  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 26-October 09

printf to cout

Posted 19 November 2009 - 11:11 PM

ok i have written this code using printf and I dont know how I can change it to cout.Can someone help me with that.Thank You

	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");
	}
	


Is This A Good Question/Topic? 0
  • +

Replies To: printf to cout

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: printf to cout

Posted 19 November 2009 - 11:24 PM

You could try learning about C++ I/O.

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.
Was This Post Helpful? 0
  • +
  • -

#3 thebeast91  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 26-October 09

Re: printf to cout

Posted 19 November 2009 - 11:43 PM

View PostOler1s, on 19 Nov, 2009 - 10:24 PM, said:

You could try learning about C++ I/O.

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
Was This Post Helpful? 0
  • +
  • -

#4 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,402
  • Joined: 13-June 08

Re: printf to cout

Posted 20 November 2009 - 12:16 AM

View Postthebeast91, on 19 Nov, 2009 - 10:43 PM, said:

View PostOler1s, on 19 Nov, 2009 - 10:24 PM, said:

You could try learning about C++ I/O.

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;


Was This Post Helpful? 1
  • +
  • -

#5 thebeast91  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 26-October 09

Re: printf to cout

Posted 20 November 2009 - 12:37 AM

View Postccubed, on 19 Nov, 2009 - 11:16 PM, said:

View Postthebeast91, on 19 Nov, 2009 - 10:43 PM, said:

View PostOler1s, on 19 Nov, 2009 - 10:24 PM, said:

You could try learning about C++ I/O.

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
Was This Post Helpful? 0
  • +
  • -

#6 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,402
  • Joined: 13-June 08

Re: printf to cout

Posted 20 November 2009 - 12:41 AM

Which parts are hard to get? It's pretty straight forward. If it was me I would just use an array to store the correct answers then read in the students answers and go through each student and match them to the answer array. In my mind, that's the quickest and least intrusive way.

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

Was This Post Helpful? 0
  • +
  • -

#7 thebeast91  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 26-October 09

Re: printf to cout

Posted 20 November 2009 - 12:43 AM

View Postccubed, on 19 Nov, 2009 - 11:41 PM, said:

Which parts are hard to get? It's pretty straight forward. If it was me I would just use an array to store the correct answers then read in the students answers and go through each student and match them to the answer array. In my mind, that's the quickest and least intrusive way.

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
Was This Post Helpful? 0
  • +
  • -

#8 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,402
  • Joined: 13-June 08

Re: printf to cout

Posted 20 November 2009 - 12:50 AM

Not really. I'm good at spotting code errors, but only if I know what i'm looking for. in your case, there are no syntax errors, so I'd have to know exactly what isn't working so I know where to start. Then I could probably tell you what's up.
Was This Post Helpful? 0
  • +
  • -

#9 thebeast91  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 26-October 09

Re: printf to cout

Posted 20 November 2009 - 12:55 AM

View Postccubed, on 19 Nov, 2009 - 11:50 PM, said:

Not really. I'm good at spotting code errors, but only if I know what i'm looking for. in your case, there are no syntax errors, so I'd have to know exactly what isn't working so I know where to start. Then I could probably tell you what's up.

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;
} 

Was This Post Helpful? 0
  • +
  • -

#10 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,402
  • Joined: 13-June 08

Re: printf to cout

Posted 20 November 2009 - 01:09 AM

It's this line.
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.
Was This Post Helpful? 0
  • +
  • -

#11 thebeast91  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 26-October 09

Re: printf to cout

Posted 20 November 2009 - 01:46 AM

View Postccubed, on 20 Nov, 2009 - 12:09 AM, said:

It's this line.
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;
}

Was This Post Helpful? 0
  • +
  • -

#12 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,402
  • Joined: 13-June 08

Re: printf to cout

Posted 20 November 2009 - 09:54 AM

To be honest with you, I would just read in all the answers into one array and then go through each line of the student answers file. Get an ID, get their answers then go through and check each one. I would also do the output at this stage as well.

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.
Was This Post Helpful? 0
  • +
  • -

#13 thebeast91  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 26-October 09

Re: printf to cout

Posted 21 November 2009 - 10:57 PM

View Postccubed, on 20 Nov, 2009 - 08:54 AM, said:

To be honest with you, I would just read in all the answers into one array and then go through each line of the student answers file. Get an ID, get their answers then go through and check each one. I would also do the output at this stage as well.

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;
}  

Was This Post Helpful? 0
  • +
  • -

#14 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,402
  • Joined: 13-June 08

Re: printf to cout

Posted 22 November 2009 - 12:43 AM

You're missing the while loop's closing brackets.

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

Was This Post Helpful? 0
  • +
  • -

#15 thebeast91  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 26-October 09

Re: printf to cout

Posted 22 November 2009 - 09:47 PM

View Postccubed, on 21 Nov, 2009 - 11:43 PM, said:

You're missing the while loop's closing brackets.

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
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2