Struck with input
Nov 21, 2019 at 8:38am UTC
I'm in the process of creating a code. Where I can enter student number, name and age. It works as long as I just type in a first name like Mark, if I type in a first name and a last name like Mark Webber then everything will go worng, can anyone help me?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
struct student_t
{
char s[50];
int a;
int i;
};
int main() {
student_t p1;
printf("Student number?" );
scanf("%d" ,&p1.i);
printf( "Name?" );
scanf("%s" ,&p1.s);
printf("age?" );
scanf("%d" ,&p1.a);
printf("Student id: %d" ,p1.i);
printf("\n Name: %s" ,p1.s);
printf("\n Age: %d" , p1.a);
return 0;
Last edited on Nov 21, 2019 at 8:38am UTC
Nov 21, 2019 at 9:33am UTC
Just use C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
struct student_t
{
string name;
int age;
int id;
};
int main() {
student_t p1;
cout << "Student number?" ;
cin >> p1.id;
cout << "Student name?" ;
cin.ignore(); // clear the endline that's in the input buffer
std::getline (cin, p1.name);
cout << "Student age?" ;
cin >> p1.age;
cout << "Student id: " << p1.id << '\n'
<< "Student name: " << p1.name << '\n'
<< "Student age: " << p1.age << '\n' ;
return 0;
}
Topic archived. No new replies allowed.