Answer the question
In order to leave comments, you need to log in
Why is the gets_s line reading function skipped?
The interface for selecting a certain function is displayed for the user (in this case, I left only the addFilm () function, because it interests me. When the addFilm () function is called in the switch statement, a request is displayed to read the data (movie name, director, etc.) For some reason, if you call the function in the switch, then "Name-> Director->" is displayed. That is, the process of reading the string name is skipped and only the string director is read. If you call setFilm () or addFilm () in the scope of the main function (not in switch), then everything is fine. Changing gets_s to cin - normally reads the first line (name), BUT it is read without spaces, and in this case, when calling gets_s() for director - the line director is already skipped. Someone can explain what kind of magic and how to deal with it?
#include <iostream>
#include <conio.h>
#define MAX_CHAR 100
using namespace std;
//жанр
enum Genre
{
anime, biographical, action, western, military, detective, baby, documentary,
drama, historical, kinokomiks, comedy, concert, short_film, crime, melodrama,
mystic, music, cartoon, musical, _scientific, adventure, reality_show, family,
sport, talk_show, thriller, horror, fantastic, film_noir, fantasy
};
struct Film
{
char * name;//название фильма
char * director;//режиссер
Genre genre;//жанр
double rating;//рейтинг
double price;//цена
};
// инициализирует/перезаписывает получаемый объект
void setFilm(Film & film)
{
cout << "Name-> ";
char name[MAX_CHAR];
gets_s(name ,MAX_CHAR);
//film.name = name;
film.name = new char[strlen(name) + 1];
strcpy_s(film.name, strlen(name) + 1, name);
cout << "Director-> ";
char director[MAX_CHAR];
gets_s(director, MAX_CHAR);
//film.director = director;
film.director = new char[strlen(director) + 1];
strcpy_s(film.director, strlen(director) + 1, director);
//считывание жанра
showGenres();
unsigned short genreId;
do
{
cout << "\nGenre id-> ";
cin >> genreId;
} while (!setGenre(film, genreId));//выход только если жанр удачно установлен
cout << "Rating-> ";
cin >> film.rating;
cout << "Price-> ";
cin >> film.price;
}
//выводит всю информацию о фильме
void showFullFilmInfo(Film film)
{
cout << "\nName-> " << film.name;
cout << "\nDirector-> " << film.director;
cout << "\nGenre-> ";
cout << "genre";// вместо "showGenreOfFilm(film);". Не хочу засорять код ненужными для данного вопроса функциями
cout << "\nRating-> " << film.rating;
cout << "\nPrice-> " << film.price << endl;
}
//добавляет объект в конец массива
Film * addFilm(Film * films, int & size)
{
system("cls");
Film * nfilms = new Film[size + 1];
Film obj;
setFilm(obj);
for (int i = 0; i < size; i++)
nfilms[i] = films[i];
nfilms[size++] = obj;
return nfilms;
}
void main()
{
int size = 5;
Film * films = new Film[size];
films[0] = { "Interstellar", "Christopher Nolan", fantastic, 8.607, 10.55};
films[1] = { "The Silence of the Lambs", "Jonathan Demme", horror, 8.349, 4.52 };
films[2] = { "Cartel Land", "Matt Heineman", documentary, 6.789, 8.99 };
films[3] = { "Citizenfour", "Laura Poitras", documentary, 7.203, 11.5 };
films[4] = { "In the Mouth of Madness", "John Carpenter", horror, 6.994, 4.52 };
//первый тестовый фрагмент кода
/*setFilm(films[0]);
showFullFilmInfo(films[0]);
_getch();*/
//второй фрагмент кода
int func_id;
do
{
cout << "1-show films list\n"
<< "2-add film in list\n"
<< "3-show full film information\n"
<< "4-search by name\n"
<< "5-search by director\n"
<< "6-search by genre\n"
<< "7-show most popular film with genre\n"
<< "8-exit\n\n";
cout << "Function number-> ";
cin >> func_id;
switch (func_id)
{
case 1:
showFilms(films, size);
break;
case 2:
cout << "films = addFilm(films, size)";
break;
case 3://show full film information
//get number(id) of film
/*int id;
for (int i = 0; i < size; i++)
cout << i << '-' << films[i].name << endl;
cout << "id-> ";
cin >> id;
system("cls");*/
cout << "showFullFilmInfo(films[id])";
break;
case 4:
cout << "searchByName(films, size)";
break;
case 5:
cout << "searchByDirector(films, size)";
break;
case 6:
cout << "searchByGenre(films, size)";
break;
case 7:
cout << "showMostPopularFilmWithGenre(films, size)";
break;
case 8:
exit(1);
}
} while (true);
}
Answer the question
In order to leave comments, you need to log in
The gets_s() call skips because when you type in cin, you press Enter at the end. When you press Enter, a newline character is sent to the stream and remains there. And then gets_s() sees it and treats it as the input string. Therefore, you must execute cin.get() after cin
#include <iostream>
using namespace std;
int main()
{
int n;
char c;
cin >> n;
cin.get(c);
cout << n << " " << int(c) << endl;
return 0;
}
[[email protected] cpp]$ .iso++ t.cpp -o t
[[email protected] cpp]$ ./t
123
123 10
[[email protected] cpp]$
The main problem is mixing formatted and unformatted input: stackoverflow.com/a/21567292/3410396
And yes, don't mix C and C++. Use getline instead of gets_s
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question