Need help with this for my project!

Hello guys I need help with these.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string.h>
using namespace std;

int mainchoice;
char bookname[100];
void tfiosdetails();
int main()
{
	system("Color 1B");
	A:
	cout << "What do you want to do?" << endl;
	cout << "[1] - Search an available book" << endl;
	cout << "[2] - Exit the program" << endl;
	cout << "Choice: ";
	cin >> mainchoice;
	if (mainchoice == 1)
		{
			system("cls");
			cout << "Enter book name: ";
			cin >> bookname;
			if(strcmp(bookname, "fault in our stars") == 0) 
			{
				system("cls");
        		        cout << "It works";
                                system("pause");
                         }

strcmp only allows me to compare one word only but I need to compare a sentence. This is a program that allows user to search for a title of a book. Thank you guys!
Last edited on
strcmp only allows me to compare one word

No, strcmp() allows you to check if one C-string to another C-string even if the C-strings have many "words".

This is a program that allows user to search for a title of a book.

First the code you posted is not a program, it is a snippet from a program. Second the snippet you posted will not search for a title of a book. It only looks to see if one string is equal to "fault in our stars". Since you don't show how you defined and assigned a value to song I can't tell if it will ever work.

By the way your variable sentence will only contain one word, not a sentence.

I have updated the source code! Help me please! Thanks!
@gentleguy can you write the full code? I don't quite get it. Thanks!
You can read here on istream::getline, which talks about using cin.getline: http://www.cplusplus.com/reference/istream/istream/getline/

This looks eerily like a homework assignment.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string.h>


int main()
{
  char input[100]{ '0' }; //Arbitrary initialization

  std::cout << "Enter something: ";
	
  std::cin.getline(input, 100);

  std::cout << "Entered: \"" << input << "\"" << std::endl;
}


Using just cin will stop reading at whitespace, so it'll only capture the first word.
cin.getline will read up to the length provided (second argument) minus 1 or up to a specified delimiter (which is, be default, the newline character), whichever comes first.

But keep in mind that only 99 characters will be read, because one character is reserved for affixing '\0' to the end of what was read. Entering more than 99 characters will cause only 99 characters to be read, the fail bit to be set, and future calls to fail unless you clear it.
It always exit the program guys!
It keeps on exiting the program :( Who can I add on facebook to help me? I really need help!
@JayhawkZombie: Bro I need you! Check you messages! I PMed you!
I still need help!
Do not give full homework solutions, this is not a homework solution website.

OP, what issue are you having now? Is it printing output or does it seem to be crashing?
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    int mainchoice;
    char bookname[100];
    
    cout << "What do you want to do?" << endl;
    cout << "[1] - Search an available book" << endl;
    cout << "[2] - Exit the program" << endl;
    cout << "Choice: ";
    cin >> mainchoice;
    
    if (mainchoice == 1)
    {
        cout << "Enter book name: ";
        cin.ignore(1000,'\n'); // <---
        cin.getline(bookname, 100); // as recommended by gentleguy
        
        if(strcmp(bookname, "team effort") == 0)
        {
            cout << "It works!!\n";
        }
    }
    else
    {
        cout << "We're all done here ...\n";
    }
    
    return 0;
}
What do you want to do?
[1] - Search an available book
[2] - Exit the program
Choice: 1
Enter book name: team effort
It works!!
Program ended with exit code: 0
Last edited on
Thank you guys!! I can finally finish my project! :)
Topic archived. No new replies allowed.