Here is my problem, I need to make a char array with 20 spots called Name, in the code below I have done this, but its not working correctly and I cant get my compiler to get it to run any other way when it prints its all messed up. Can someone show me where I am going wrong? Also for the float variable GPA it works if i input 4.3, but if I input 4.0, it drops the decimal and zero and prints just a 4. here is my code
Hi, for the first problem with the character array, I think you would have to change it in the code's initialization from 'jonas'; to 'j', 'o', 'n', 'a', 's'; But in the user input when the program is running, it should work correctly.
As for the decimal problem, just add #include <iomanip> at the top, and use the fixed and setprecision(1) manipulators right before it cout's the value for the GPA.
#include <iostream>
#include <string>
#include <iomanip>
using std::setprecision;
using std::fixed;
usingnamespace std;
struct StudentRecord {
char Name[20];
int ID;
float GPA;
};
int main()
{
StudentRecord TESCStudent;
TESCStudent.Name[20] = 'j','o','n','a','s';
TESCStudent.ID = 1234;
TESCStudent.GPA = 4.0;
cout.precision(1);
cout<< "Name = " << TESCStudent.Name;
cout<< "\nID = " << TESCStudent.ID;
cout<< "\nGPA = " << fixed << showpoint << TESCStudent.GPA;
StudentRecord NewStudent;
cout<< "\nPlease Enter your name ";
cin>> NewStudent.Name;
cout<< "\nPlease Enter your ID ";
cin>> NewStudent.ID;
cout<< "\nPlease Enter your GPA ";
cin>> NewStudent.GPA;
cin.ignore();
cout<< "\nName = " << NewStudent.Name;
cout<< "\nID = " << NewStudent.ID;
cout<< "\nGPA = " << fixed << showpoint << NewStudent.GPA;
cin.ignore();
}
I used the showpoint command. But as for the char array problem, I tried that before and I get a bunch of odd characters displayed, but when it runs the second time it works. so clearly the problem is with the definition part, I am still trying to get to work. Can anybody tell me why i cannot define this char array?