I cannot debug my program

my IDE is telling me to take out a parenthase on lines 15 and 17, if I do this my program simply wont work! please help me

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Lab 9D
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
    string stringOne, stringTwo;
    
	cout << "Please enter a sentence: ";
	getline(cin,stringOne);
	cout << "Please enter another sentence: ";
	getline(cin,stringTwo);
	
	if (strcmp(stringOne,stringTwo) == 0)
	    cout << "The strings are equal" << endl;
	else if (strcmp(stringOne,stringTwo) < 0)
	    cout << "String one comes before string two" << endl;
	else
	    cout << "String one comes after string two" << endl;
	    
	return 0;
}
You are not debugging, you are compiling.

my IDE is telling me to take out a parenthase on lines 15 and 17, ....


It is not telling you that:

15:32: error: cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'int strcmp(const char*, const char*)'


Don't use the C strcmp function on a C++ string. Just use the operator== which is part of std::string:

1
2
3
if (stringOne == stringTwo ) {
    //   ......
}


std::string also has operators < and >
Last edited on
the instructions tell me to use it
If you are using c-strings:

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
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    char stringOne[80];
    char stringTwo[80];
    
    cout << "Please enter a sentence: ";
    cin.getline(stringOne, 80);
    
    cout << "Please enter another sentence: ";
    cin.getline(stringTwo, 80);
    
    if (strcmp(stringOne,stringTwo) == 0)
        cout << "The strings are equal" << endl;
    else if (strcmp(stringOne,stringTwo) < 0)
        cout << "String one comes before string two" << endl;
    else
        cout << "String one comes after string two" << endl;
        
    return 0;
}


OR:
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string stringOne;
    string stringTwo;
    
    cout << "Please enter a sentence: ";
    getline(cin, stringOne);
    
    cout << "Please enter another sentence: ";
    getline(cin, stringTwo);
    
    if (stringOne == stringTwo)
        cout << "The strings are equal" << endl;
    else if (stringOne < stringTwo)
        cout << "String one comes before string two" << endl;
    else
        cout << "String one comes after string two" << endl;
        
    return 0;
}


But not both combined without a extra tedious and unnecessary code.

http://www.cplusplus.com/reference/istream/istream/getline/
http://www.cplusplus.com/reference/cstring/strcmp/?kw=strcmp

http://www.cplusplus.com/reference/string/string/getline/?kw=getline
http://www.cplusplus.com/reference/string/string/operators/


Last edited on
the instructions tell me to use it
- can we get a full copy of the instructions? Perhaps they meant for you to use - [ http://www.cplusplus.com/reference/string/string/compare/ ]? TheIdeasMan has a recommend on using String's comparison operators which would be easier, but the option is available.

There are instances where you might be using a C-language library inside a C++ program, so it's still good to know how to convert between String and Cstring.
String to const Cstring - [ http://www.cplusplus.com/reference/string/string/c_str/ ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Lab 9D
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
    string stringOne, stringTwo;
    
	cout << "Please enter a sentence: ";
	getline(cin,stringOne);
	cout << "Please enter another sentence: ";
	getline(cin,stringTwo);
	
	if (strcmp(stringOne.c_str(),stringTwo.c_str()) == 0)
	    cout << "The strings are equal" << endl;
	else if (strcmp(stringOne.c_str(),stringTwo.c_str()) < 0)
	    cout << "String one comes before string two" << endl;
	else
	    cout << "String one comes after string two" << endl;
	    
	return 0;
}


This link shows how to capture a non-const Cstring from a String - [ http://www.cplusplus.com/reference/string/string/copy/ ] Just be certain to add that string terminator at the end, the resulting C-string would not be safe to use without it.

Off topic, but here are some type conversions between Strings and number-types - [http://www.cplusplus.com/reference/string/ ], check out stoi(), stod(), stoll(), and to_string()

Note: I agree with Againtry: wherever possible just stick to one or the other, converting between the two mostly just adds to wasted computing time. The code example above is just an attempt to closely fit your original code. In real code try to keep string conversions near the edges of your code where interacting with a library or binary file requires it.

@Againtry: I've never really considered the difference between std::istream::getline(cstring, length) and std::getline(istream, string), but there's a possibility for fewer conversions and allocations when used correctly. Thank you, good info.
Last edited on
Topic archived. No new replies allowed.