Simon Leung
asked on
Looping problem in c++
It seems that the after displaying the book name, it can't go up to the for loop until I press "Enter" button. Any idea ?
Thx
for (int k = 0; k < noOfBook; k++) {
ibook = createBook();
cout << ibook.getName() << endl;
}
#include<iostream>
#include<vector>
#include <stdio.h>
#include <string>
using namespace std;
class Book {
private:
char name[50];
int pub_year;
int book_count;
char subject[50];
public:
//constructor
Book() {};
Book(char iname[50], int y, int count, char sub[50]) {
strcpy_s(name, iname);
pub_year = y;
book_count = count;
strcpy_s(subject, sub);
};
//getter
char* getName() {
return name;
}
int getYear() {
return pub_year;
}
int getBookcount() {
return book_count;
}
char* getSubject() {
return subject;
}
//setter
};
class Subject {
private:
char name[50];
int book_count;
Book blist[100];
public:
//constructor
Subject() {};
Subject(char iname[50]) {
strcpy_s(name, iname);
book_count = 0;
};
//getter
char* getName() {
return name;
}
//Method
void printListedDetail() {
//printf("%d %d %.2f \n", month, day, balance);
//cout << name << " " << pub_year << " " << book_count << " " << subject << endl;
}
};
std::string first_numberstring(std::string const& str)
{
char const* digits = "0123456789";
std::size_t const n = str.find_first_of(digits);
if (n != std::string::npos)
{
std::size_t const m = str.find_first_not_of(digits, n);
return str.substr(n, m != std::string::npos ? m - n : m);
}
return std::string();
}
Book createBook() {
char str[50];
char book[50];
char year[50];
char number[50];
char subject[50];
char out[50];
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Input the information of books (name, pub_year, number, subject):";
cin.getline(str,50);
cin.clear();
int i = 0;
//Book
do {
book[i] = str[i];
i++;
} while (str[i] != ' ');
book[i] = '\0';
//Year
i++;
do {
year[i] = str[i];
i++;
} while (isdigit(str[i]) && str[i] != ' ');
year[i] = '\0';
int iYear = stoi(first_numberstring(year));
//number
i++;
do {
number[i] = str[i];
i++;
} while (isdigit(str[i]) && str[i] != ' ');
number[i] = '\0';
int inumber = stoi(first_numberstring(number));
//subject
i++;
do {
subject[i] = str[i];
i++;
} while (str[i] != '\0');
subject[i] = '\0';
Book ibook = Book(book, iYear, inumber, subject);
return ibook;
}
int main()
{
Subject library[50];
int totSubject = -1;
int noOfBook;
Book ibook;
char choice = 'a';
while (true)
{
cout << "welcome to the Library, what do you want to do ? " << endl;
cin >> choice;
switch (toupper(choice))
{
case 'S':
cout << "Input the number of books :";
cin >> noOfBook;
for (int k = 0; k < noOfBook; k++) {
ibook = createBook();
cout << ibook.getName() << endl;
}
break;
case 'L':
break;
case 'B':
break;
case 'Q':
exit(0);
default:
cout << "Invalid Input " << endl;
break;
}
}
return 0;
}
ASKER
Thx. Actually, this is a requirement (use C-string) to increase the diffculty of the question.....
ASKER
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Input the information of books (name, pub_year, number, subject):";
cin.getline(str,50);
cin.clear();
add cn.clear() at the beginning but doesn't help..
Thx again
Recall post in previous question. Look at indexing for year - it is iY, not i.
//Year
char year[8];
size_t iY = 0;
++i;
do {
year[iY++] = str[i];
++i;
} while (isdigit(str[i]) && str[i] != ' ');
year[iY] = '\0';
cout << year << endl; // year has a c-style string "1995" in it.
uint32_t yearInt = atoi(year);
cout << yearInt << endl;
ASKER
Thx but still can't fix the problem... still need to need to pass "Enter" to display input messasge.
>> this is a requirement (use C-string) to increase the diffculty of the question
If that is the case, then are you really allowed to use std::string?
How are you expected to handle a book title that has spaces in it?
If that is the case, then are you really allowed to use std::string?
How are you expected to handle a book title that has spaces in it?
ASKER
"You cannot use #include<string> to initialize a string object, you should define
the character array like: char_array[50] to get some input. But you can use the
functions like strcpy and strtok, etc. in #include<cstring>."
Suppose there is not space between book title..
thx
the character array like: char_array[50] to get some input. But you can use the
functions like strcpy and strtok, etc. in #include<cstring>."
Suppose there is not space between book title..
thx
>> ... in #include<cstring>.
OK, but you are using:
Even in C++, I usually used C-style I/O converting strings to char* as needed in printf statements.
BTW - you should clean up the indexing of the destination c-style strings as mentioned above and in previous question. That is, the index i should not be used for both source and destination.
BTW - In Visual Studio 2019, your program compiles. But using g++, it does not. What are you using?
OK, but you are using:
#include <string>
This cin.ignore is tricky - never had to use it before.Even in C++, I usually used C-style I/O converting strings to char* as needed in printf statements.
BTW - you should clean up the indexing of the destination c-style strings as mentioned above and in previous question. That is, the index i should not be used for both source and destination.
BTW - In Visual Studio 2019, your program compiles. But using g++, it does not. What are you using?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
"You cannot use #include<string> to initialize a string object, you should defineGeez, another dinosaure teacher teaching C++ as a subset of C ...
the character array like: char_array[50] to get some input. But you can use the
functions like strcpy and strtok, etc. in #include<cstring>."
This is not an increase of difficulty, this is very bad practices !!! (to be polite).
The standard library offer a bunch of tools to handlestring, arrays, type safety ect ... Not using them is a crime.
I urge you to find correct ressources, before you get used to bad habits that you'll struggle to forget, such as C++ primer (5th edition) by Lippman.
@Fabrice,
I've seen crazier assignments at the Masters level. One complained his final project was to create a simple database in C without using any functions. Felt sorry for him. Maybe you can think of some justification for that.
About Simon's restrictions on using the string class. I can see some merit in not using it as a learning tool for two reasons. Firstly, it helps get an idea of what might be going under the hood when you have to write string-like functions in C. Secondly, in many legacy projects in the wild, you have these C-strings that you have to deal with. Maybe the instructor is trying to give the students a taste of what they will be up against.
I've seen crazier assignments at the Masters level. One complained his final project was to create a simple database in C without using any functions. Felt sorry for him. Maybe you can think of some justification for that.
About Simon's restrictions on using the string class. I can see some merit in not using it as a learning tool for two reasons. Firstly, it helps get an idea of what might be going under the hood when you have to write string-like functions in C. Secondly, in many legacy projects in the wild, you have these C-strings that you have to deal with. Maybe the instructor is trying to give the students a taste of what they will be up against.
Why those horrible characters array instead of string ?
They are nightmare to work with, give them up immediatly !!
The default constructor for the book class is just wrong, an anonymous book is a no-sens.
In modern C++, you should not use the "using namespace std" directive, this instruction's sole purpose is to ensure code written before 1998 (22 years ago !!!!) are still compatible with the standard library.
Concerning your issue, there are remaining characters in the standard input, just clear it:
Open in new window