0

Im currently going through C++ Basics in data structure and have a small doubt regarding strings

I am trying to input a string value from the main function by creating an instance of a structure object in the main function.

#include<iostream>
#include<sstream>
#include<string>

using namespace std;

struct StudentData {
  string name[50];
  char rollNo[20];
  int semester;
};

int main() {
  struct StudentData s1;
  cout<<"Enter the name, Roll.No and Semester of the student:"<<endl;
  getline(cin, s1.name);
  cin>>s1.rollNo>>s1.semester;
  cout<<endl<<"Details of the student"<<endl;
  cout<<"Name: "<<s1.name<<endl;
  cout<<"Roll.No: "<<s1.rollNo<<endl;
  cout<<"Semester: "<<s1.semester<<endl;
  return 0;
}

But here I am getting error in getline for name.

mismatched types 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>' and 'std::string [50]' {aka 'std::__cxx11::basic_string<char> [50]'}

Could you please explain what is happening here? Thank you

  • 2
    The `StudentData::name` is array of `std::string` not a single `std::string`. `std::getline(std::cin, s1.name[0]);` or `for(auto& name: s1.name) std::getline(std::cin, name);` – Ghasem Ramezani Aug 24 '21 at 04:45
  • Why does `StudentData` need to store 50 names? Why use `std::string` for the names, but not for `rollNo`? – JaMiT Aug 24 '21 at 05:02
  • Basics, consider [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/364696), `string name[50];` creates an array of `50` `std::string`, `char rollNo[20];` creates a character-array capable of holding 1 C-string of 49-characters max (plus room for the nul-terminating character) – David C. Rankin Aug 24 '21 at 06:38
  • Above should be "19-characters max" for the C-string capable of being held by `rollNo[20]`.... typo – David C. Rankin Aug 24 '21 at 06:47

2 Answers2

2

In the case of strings, memory is allocated dynamically, More memory can be allocated at run time on demand. As no memory is preallocated, no memory is wasted. So bonding it will give an error try-

struct StudentData {
string name;
char rollNo[20];
int semester;
};

if you want to still bond the input try-

using namespace std;
  struct StudentData {
  string name;
  char rollNo[20];
  int semester;
 };

  int main() {
    struct StudentData s1;
    cout<<"Enter the name, Roll.No and Semester of the student:"<<endl;
    getline(cin, s1.name);

    while(s1.name.size()>50){
        string word;
        cout<<"Invalid!"<<endl;
        cout<<"enter-name again"<<endl;
        getline(cin, word);
        s1.name = word;
        cout<<s1.name.size()<<endl;
    }
    cin>>s1.rollNo>>s1.semester;
    cout<<endl<<"Details of the student"<<endl;
    cout<<"Name: "<<s1.name<<endl;
    cout<<"Roll.No: "<<s1.rollNo<<endl;
    cout<<"Semester: "<<s1.semester<<endl;
    return 0;
  }
1

When you get to getline(cin, s1.name); , is is compiled to an address which contains the start of an array of string objects so the computer tries to write a string of characters to the location of a String class in memory.

This will not work because the memory is allocated to not just hold an ascii character.

I believe you are confusing string with char [] array.

gorilla bull
  • 150
  • 9