Char array and floats

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

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
35
36
37
38
39
40
41
42

#include <iostream>


using namespace std;

struct StudentRecord {         
  char Name[20];
  int ID;
  float GPA;
};

int main()
{
  StudentRecord Student; 
  
  Student.Name[20] = 'jonas'; // I belive the problem is here
  Student.ID = 1234;
  Student.GPA = 4.0;
  
  cout<< "Name = " << Student.Name[20];
  cout<< "\nID = " << Student.ID;
  cout<< "\nGPA = " << Student.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 = " << NewStudent.GPA;
  
  cin.ignore();
 
}
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.

Here's the edited code:
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
35
36
37
38
39
40
41
42
#include <iostream>
#include <iomanip>


using namespace std;

struct StudentRecord {         
  char Name[20];
  int ID;
  float GPA;
};

int main()
{
  StudentRecord Student; 
  
  Student.Name[20] = 'j', 'o', 'n', 'a', 's'; // I belive the problem is here
  Student.ID = 1234;
  Student.GPA = 4.0;
  
  cout<< "Name = " << Student.Name[20];
  cout<< "\nID = " << Student.ID;
  cout<< "\nGPA = " << fixed << setprecision(1) << Student.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 << setprecision(1) << NewStudent.GPA;
  
  cin.ignore();
 
}


Normally you would need to reset the setprecision if you wanted to display a number without the decimal, but it works correctly there.
This is how I fixed the decimal problem:
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
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
#include <iomanip>
using std::setprecision;
using std::fixed;
using namespace 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?
Topic archived. No new replies allowed.