Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
First code:
int a;
	long b;
	char c;
	float d;
	double e;

	scanf("%d", &a);
	scanf("%ld", &b);
	scanf("%c", &c);
	scanf("%f", &d);
	scanf("%lf", &e);

	printf("%d\n", a);

	printf("%ld\n", b);

	printf("%c\n", c);

	printf("%f\n", d);

	printf("%lf\n", e);


Second Code:

int a; 
    long b; 
    char c; 
    float d; 
    double e;
    scanf("%d %ld %c %f %lf",&a,&b,&c,&d,&e);

    printf("%d", a);
    cout<<endl;
    printf("%ld",b);
    cout<<endl;
    printf("%c ", c);
    cout<<endl;
    printf("%f ", d);
    cout<<endl;
    printf("%lf", e);


What I have tried:

first one is not working and second one is giving correct outputs on these set of input values:
3 12345678912345 a 334.23 14049.30493
Posted
Updated 23-Jun-19 1:42am

Because your second version explicitly includes the spaces which separate your values, your first doesn't, so it gets out of sync.
Try this:
C++
int a;
long b;
char c;
float d;
double e;

scanf("%d ", &a);
scanf("%ld ", &b);
scanf("%c ", &c);
scanf("%f ", &d);
scanf("%lf", &e);

printf("%d\n", a);
printf("%ld\n", b);
printf("%c\n", c);
printf("%f\n", d);
printf("%lf\n", e);
 
Share this answer
 
Then use the third (C++) one:
C++
#include <iostream>
using namespace std;

int main()
{
  int a;
  long b;
  char c;
  float d;
  double e;

  cin >> a >> b >> c >> d >> e;
  if ( cin.good())
    cout << a << " " << b << " " << c << " " << d << " " << e << endl;
  else
    cout << "no luck" << endl;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900