Hey. There is a structure where we have to write information about the owner, full name and phone number .. If you enter> 1 words in the full name (separated by a space, i.e. first name, last name), then he will not ask for the number, but THEN, when deducing in the num number field, it will display the second word of the full name (that after the space).

It is necessary that the program can read a few words ... Thanks in advance. Code:

using namespace std; struct owner { char FIO [19], num[9]; }a[9]; int main () { cout << "BBEDuTE KOJIu4ECTBO BJIADEJIbTCEB: "; int n; cin >> n; for (int i=0; i<n; i++) { cout << "\n BJIADEJIETC[" << i+1<<"]"; cout << "\n FIO "; cin >> a[i].FIO; cout << " num "; cin >> a[i].num; } for (int i=0; i<n; i++) { cout << "\n BJIADEJIETC[" << i+1<<"]"; cout << "\n FIO "; cout << a[i].FIO; cout << " num "; cout << a[i].num; } } 

instead of cin I tried cin.get(a[i].FIO) and gets(a[i].FIO) , but gets(a[i].FIO) error ... I tried the string with getline (cin, a[i].FIO) is also an error. ..

PS: I understand that cin reads only 1 word, but I don’t know how to fix it.

  • one
    insert setlocale (LC_ALL, "rus") at the beginning of mein and do not write crackers - Max Zhukov

4 answers 4

@Erlotaza , the easiest way is to solve your problem by changing the format of the input data.

Perfectly suitable format of the form:

 123-4567 Иванов И.И. 

those. in one line, first the number, and then the full name.

Then data entry might look something like this:

 .... string phone, fname; while (cin.good()) { cout << "Phone FullName :"; cin >> phone; getline(cin, fname); if (!cin.good()) break; cout << "phone: " << phone << " fname: ["<< fname << "]\n"; .... } .... 

Note only that when reading the name of the getline() function, there will be spaces at the beginning and end (if entered there) of the string fname.

The following code can easily remove them.

  int beg = fname.find_first_not_of(" \t"), fin = fname.find_last_not_of(" \t"); fname = fname.substr(beg, fin-beg+1); 

    If you cannot change the structure of data reception, you can read the line to the end, write the last word of it as a phone and the remaining data as a full name.

      #include<iostream> #include<string> using namespace std; void parse(string s,string &fio,string &phone) { phone=""; fio=""; int i,l=s.length(); for(i=l-1;i>=0;i--) { if(s[i]==' '&&phone.length()) break; //если мы дошли до пробела и в строке телефона уже что то записано phone+=s[i]; } reverse(phone.begin(),phone.end()); //т.к. мы шли с конца, надо развернуть написанный телефон fio=s; fio.resize(l-phone.length()); } int main() { string str,fio,phone; int N; cin >> N ; getline(cin,str); while(N--) { getline(cin,str); parse(str,fio,phone); cout << "fio: " <<fio<<" phone: "+phone << endl; } return 0; } 

      avp Thank you, but I need to know in the structure, separately with the string, what works, I indicated in the getline question (cin, a [i] .num) that does not work:

       [C++ Error] Unit1.cpp(21): E2285 Could not find a match for 'getline<_CharT,_Traits,_Alloc>(istream,string *)' 

      BogolyubskiyAlexey , Thank you, but: 1) #include <algorithm> 2) DOES NOT WORK as it should: if you enter a symbol word, an infinite loop starts with deduction, if you enter the numbers and press enter, you can enter the second line, but then output only the second .....


      THE PROBLEM IS SOLVED; it was necessary to clear the stream using the cin.ignore () function; and all) But try it yourself guess to this ((

       for (int i = 0; i < k; i++) { cout << "Owner " << i+1 << " owner\'s name: "; cin.ignore(); getline(cin,owners[i].name); cout << "Enter " << i+1 << " owner\'s number: "; cin >> owners[i].number; cout << endl; } 
      • @Erlotaza, specify the input format you need. On the first line of the name, on the next phone? And so N times in the loop? And the data should fill exactly your structure? - avp
      • @Erlotaza, to format the code in the question-answer it should be separated with empty lines from the rest of the text, select (selection with the mouse) and press the {} button in the top line of the editor. - In general, in C ++, the transition from element by element input to line by line is an eternal problem. In practice, it is better not to mix them. Read line by line, read line numbers (usually we read from the file) for sensible diagnostics and parse the read data yourself. Anyway, in practice, it is necessary to monitor overflow. - By the way, in the example in your answer, did you replace char[] with string in the structure? - avp

      if you are programming in C ++, you should use the string type instead of char. because it is not known how long it will take for the full name.

       #include <iostream> #include <string> using namespace std; struct owner{ string FIO, num; } a[3]; string f, ii, o; int main() { for (int i = 0; i < 3; i++){ cin >> f >> ii >> o >> a[i].num; a[i].FIO += f; a[i].FIO += " "; a[i].FIO += ii; a[i].FIO += " "; a[i].FIO += o; } cout << "_____________________________" << endl; for (int i = 0; i < 3; i++){ cout << a[i].FIO << " T-" << a[i].num << endl; } return 0; } 

       jdfksdh sdjfh ksdj 45465 sdjfjdsfkj sdkfjsdkjf dskfsdjf 45625 skdfjkdsjf ajkdksjfkd skjdfsdfj 4569 _____________________________ jdfksdh sdjfh ksdj T-45465 sdjfjdsfkj sdkfjsdkjf dskfsdjf T-45625 skdfjkdsjf ajkdksjfkd skjdfsdfj T-4569 Для продолжения нажмите любую клавишу . . . 
      • @perfect, and what will your program do if, say, there is no middle name? - avp
      • write word unknown or dash - perfect
      • And in my opinion, she will write down the phone number instead of the middle name and the next last name instead of the number. Those. if the strict input format is not observed, then there is no chance at all of it to correctly continue data entry. - IMHO, it is these considerations and the problems encountered with line-by-line input (more precisely, a mixture of element-by-element and line-by-line input) caused this question. - avp 5:57 pm
      • if there are fewer than four input lines, the fourth argument will be expected (that is, warning the user about the error), it is advisable to display a brief help with the input rules when starting the program, or describe how to work with this program if it is nix-os. But in general, in this issue the database smells. so if there is no middle name, we write the word "no" instead of it, and on this basis in the database, you can select everyone who has not fully completed their full name. - perfect