Char array and floats


I'm working on a previous thread and have an issue with the char array printing a bunch of weird symbols for line 20. I've been trying different things like putting a null after the last 'r' to get it to run correctly but can't figure it out any help would be appreciated. thanks

original post:
http://www.cplusplus.com/forum/beginner/1025/

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
47
48
49
#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] = 'S','u','p','e','r','P','r','o','g','r','a','m','m','e','r';
  TESCStudent.ID = 1234;
  TESCStudent.GPA = 4.0;
  
  cout.precision(1);
  cout<< "Name = " << TESCStudent.Name[20];
  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();
 
}
Last edited on
Lines 5-6: These are unnecessary, since you bring in the entire std namespace at line 7.

Line 11: You're declaring Name to hold 20 characters. These positions are [0]-[19]. Use std::string instead.

Line 20: Two problems here.
1) You're trying to store a character into Name[20]. That's an out of bounds reference. Nothing is stored into Name[0]-Name[19].
2) You're using the comma operator. You're not storing a list of characters into Name. You're storing only the last character ('r'), which is out of bounds per #1.

Line 25: Again, you're making an out of bounds reference to Name[20].

Line 47: return 0; missing.

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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

struct StudentRecord 
{   string Name;
    int ID;
    float GPA;
};

int main()
{   StudentRecord TESCStudent; 

  TESCStudent.Name = "SuperProgrammer";
  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();
    return 0; 
}

Last edited on
closed account (48bpfSEw)
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
47
48
49
#include <iostream>
#include <string>
#include <iomanip>
#include <cstring>  // strcpy

using std::setprecision;
using std::fixed;
using namespace std;

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

int main()
{
  StudentRecord TESCStudent; 

  std::strcpy(TESCStudent.Name, "Superprogrammer");  // bug 1
  TESCStudent.ID = 1234;
  TESCStudent.GPA = 4.0;
  
  cout.precision(1);
  cout<< "Name = " << TESCStudent.Name; // bug 2
  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();
 
}

Last edited on
closed account (48bpfSEw)
Attention: the comma in CPP is a bit tricky!

It is legitim to write

1
2
3
4
5
for (int i1=0, int i2=0;  
      i1 < 0; 
      i1++) {
 i2 += i1;
}



This is legitim too
 
Name[20] = 'S','u','p','e','r';


but has a lot of erros.

The aim is to initialize the array of chars with "Super", right?

The first error is: Name is an array with 20 characters, beginning with the index 0 up to 19 (count = 20). If you like to write beyond this range you'll overwrite memory and your app can crash.

You can not assign a string to an array of characters on this way you did.

Either you write the initialisation in this manner

1
2
3
Name[0] = 'S';
Name[1] = 'u';
...


or with the function strcpy


Topic archived. No new replies allowed.