954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

COUT Convert to PRINTF

can u help me convert the cout to printf..i dont have a clue how..
tnx

#include<iostream.h>
#include<string.h>
#include<conio.h>
int findSimilar(char[],char[]);
void checkValue(int,int);
int main(){
	char boy[50];
	char girl[50];
	int similarInBoy=0;
	int similarInGirl=0;
	int total;
	clrscr();
	cout<<"Enter Boy's Name:  ";
	cin.getline(boy,50);

	cout<<"Enter Girl's Name: ";
	cin.getline(girl,50);

	similarInBoy=findSimilar(boy,girl);
	similarInGirl=findSimilar(girl,boy);
	total=similarInGirl+similarInBoy;

	cout<<"Boy: "<<similarInBoy<<",";
	checkValue(similarInBoy,1);
	cout<<"\n";

	cout<<"Girl: "<<similarInGirl<<",";
	checkValue(similarInGirl,2);
	cout<<"\n";

	cout<<"Total: "<<total<<",";
	checkValue(total,3);
	cout<<"\n";
	getch();
	return 0;
}

void checkValue(int similar,int gender){

	if(gender==1){
		if(similar>6)
			similar%=6;
		switch(similar){
		case 1:
			cout<<"Friend";
			break;
		case 2:
			cout<<"Love";
			break;
		case 3:
			cout<<"Affair";
			break;
		case 4:
			cout<<"Married";
			break;
		case 5:
			cout<<"Enemy";
			break;
		case 6:
			cout<<"Sweetheart";
			break;

		}
	}
	else if(gender==2){
		if(similar>6)
			similar%=6;
		switch(similar){
		case 1:
			cout<<"Friend";
			break;
		case 2:
			cout<<"Love";
			break;
		case 3:
			cout<<"Affair";
			break;
		case 4:
			cout<<"Married";
			break;
		case 5:
			cout<<"Enemy";
			break;
		case 6:
			cout<<"Sweetheart";
			break;
		}
	}
	else{
		if(similar>12)
			similar%=12;
		switch(similar){
			case 1:
			cout<<"Friends";
			break;
		case 2:
			cout<<"Lovers";
			break;
		case 3:
			cout<<"Affair";
			break;
		case 4:
			cout<<"Marriage";
			break;
		case 5:
			cout<<"Enemies";
			break;
		case 6:
			cout<<"Sweethears";
			break;
		}
	}
}


int findSimilar(char firstName[],char secondName[]){
	int len;
	int len2;
	int similar=0;
	char ch1,ch2;
	char* first;
	char *second;

	first=strupr(firstName);
	second=strupr(secondName);
	len=strlen(firstName);
	len2=strlen(secondName);

	for(int ctr=0;ctr<len;ctr++){
		ch1=first[ctr];
		for(int ctr2=0;ctr2<len2;ctr2++){
			ch2=second[ctr2];
			if((ch1!=' ')&&(ch1==ch2)){
				similar++;
				break;
			}
		}
	}
	return similar;
	}
Shodow
Junior Poster
115 posts since Jan 2011
Reputation Points: 14
Solved Threads: 0
 

Look up the format of printf(). Use that info to do your conversion.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

my bad help me convert cout to printf and cin to scanf.. help me pls asap

Shodow
Junior Poster
115 posts since Jan 2011
Reputation Points: 14
Solved Threads: 0
 
my bad help me convert cout to printf and cin to scanf.. help me pls asap


here you go:

int printf(const char *format, ...)

But you have to include header file "stdio.h"("cstdio.h" if you're compiling as c++) in your code to use printf or scanf.
simple example of printf:

printf("Hello, world!\n")


Here's more about printf and scanf .

Use fgets instead , since scanf is not considered safe .

diwakar wagle
Posting Whiz in Training
203 posts since Nov 2008
Reputation Points: 48
Solved Threads: 31
 
Use fgets instead , since scanf is not considered safe.


scanf() is perfectly safe when used correctly. The problem is things like %s without a field width where the programmer has no clue that it's a buffer overflow waiting to happen.

If scanf() is so unsafe, then why is the recommended alternative a combination of fgets() and sscanf()? The only major difference between scanf() and sscanf() is the source of characters. Further, if you read those links, you'll find that the "problems" with scanf() have to do with variations of the following:Not understanding how scanf() manages whitespace
Not understanding how the %s specifier works
While I agree that an intuitive interface and semantics are both important, ignorance of scanf() isn't the fault of scanf(). Were I in a perverse mood, I'd argue that fgets() also has problems because beginners are often confused about how newlines are managed.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

can u help me with scanf? cuz its must be printf and scanf..pls

how to convert this in scanf?

cin.getline(boy,50);
Shodow
Junior Poster
115 posts since Jan 2011
Reputation Points: 14
Solved Threads: 0
 

can u help me with scanf? cuz its must be printf and scanf..pls

how to convert this in scanf?

cin.getline(boy,50);


A C programmer would prefer fgets() for string input, but since this isn't an immediately obvious question about scanf(), I'll answer it. The closest equivalent to such a getline() call with scanf() is:

scanf("%49[^\n]", boy);
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: