Need help with this for my project!

Aug 21, 2016 at 11:02am
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 Aug 21, 2016 at 10:05pm
Aug 21, 2016 at 11:44am
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.

Aug 21, 2016 at 10:02pm
I have updated the source code! Help me please! Thanks!
Aug 22, 2016 at 9:25pm
@gentleguy can you write the full code? I don't quite get it. Thanks!
Aug 22, 2016 at 11:05pm
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.
Aug 23, 2016 at 3:54am
It always exit the program guys!
Aug 23, 2016 at 6:34am
It keeps on exiting the program :( Who can I add on facebook to help me? I really need help!
Aug 23, 2016 at 6:35am
@JayhawkZombie: Bro I need you! Check you messages! I PMed you!
Aug 23, 2016 at 1:38pm
I still need help!
Aug 23, 2016 at 1:56pm
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?
Aug 23, 2016 at 3:02pm
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 Aug 23, 2016 at 3:02pm
Aug 24, 2016 at 2:24pm
Thank you guys!! I can finally finish my project! :)
Topic archived. No new replies allowed.