0

I cant get the input to get the names of countries that are more than one word. I understand that it stops at the whitespace, and I would like the option to sort by alphebetical order if I want, but I can't remember how to be able to take in whitespaces for the covid19[i].country and covid19[i].region.

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

struct CoronaVirus
{
    char country[30];
    int cases;
    int deaths;
    char region[30];
};


int main()
{
    
    ifstream list;
    list.open("country.txt");
    CoronaVirus covid19[21];
    
    for (int i = 0;i < 20;i++)
    {
        list>>covid19[i].country>>covid19[i].cases>>covid19[i].deaths>>covid19[i].region;
    }
    
    for (int i = 0;i < 20;i++)
    {
        cout<<right<<setw(10)<<covid19[i].country<<"\t"<<right<<setw(10)<<covid19[i].cases<<"\t";
        cout<<covid19[i].deaths<<"\t"<<right<<setw(10)<<covid19[i].region<<endl;
    }

    return 0;
}
JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • 1
    You are probably looking for [`std::getline`](https://en.cppreference.com/w/cpp/string/basic_string/getline). – Eljay Nov 15 '20 at 00:00
  • 2
    Shouting (writing many things in all uppercase letters) is not a good idea. – MikeCAT Nov 15 '20 at 00:02
  • Sorry im at work and we use all caps. Im not shouting my bad. – Daniel Burgess Nov 15 '20 at 00:11
  • i figured it out: i needed to have " list.getline(covid19[i].country,30,'\t'); list>>covid19[i].cases>>covid19[i].deaths; list.getline(covid19[i].region,30,'\n');" in the body of the first for loop. – Daniel Burgess Nov 15 '20 at 00:22

0 Answers0