Name: Edward 2005-06-23 19:36
Hello friends. As you may know from my previous requests for the sharing of your knowledge, I'm working my way into C and C++. As you can expect, I'm pretty bad at it, as my thing is more pure maths and philosophy (but hey, gotta try...).
Since I'm trying to tackle the areas I have difficulty, I picked out some 'trickier' (emphasizing the noobishness on my part) exercises from an O'Reilly manual. They were all going well until this one, which is leaving me stumped. I'm supposed to write a program that takes two strings as input and checks that the first one is the beginning of the second one.
I wrote the following program to do so, which isn't exactly working (always reports string A is NOT the beginning of string B), and at compile time I get the following list of warnings:
++++
stringbegin.cpp:65:69: warning: multi-character character constantstringbegin.cpp:70:30: warning: multi-character character constant
stringbegin.cpp: In function `int main()':
stringbegin.cpp:65: warning: comparison is always true due to limited range of data type
stringbegin.cpp:70: warning: comparison is always false due to limited range of data type
++++
I've included the source code of the program below, which I've commented rather profusively. I'd be ever so thankful if someone who knows what he or she is doing could take a look at this and tell me what the hell is going wrong? I'm assuming it's something dead simple and I'm just missing out on something.
Thanks in advance guys...
-- Edward.
--- Program begin ---
/************************************************
* String Begin! *
* ------------- *
* This program checks if stringA begins *
* string B or not. *
* It makes no use of the string.h header. *
* *
* Painstakingly hacked together by *
* Edward G///////////. *
************************************************/
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
//#define DEBUG
/* Debug commented for now, given it's only set up to check that strings are input correctly.*/
int main() {
char stringA[11]; // Two strings being compared.
char stringB[21]; // A is smaller than B. User is informed of this.
cout << "\nInput string A (10 chars max!): ";
cin.getline(stringA, sizeof(stringA)); //
// Debug checks that stringA is input correctly. Positive.
#ifdef DEBUG
cout << "\n## String A: " << stringA[0] << stringA[1] << stringA[2] << stringA[3] << stringA[4] << stringA[5] << stringA[6] << stringA[7] << stringA[8] << stringA[9] << stringA[10] << stringA[11] << endl;
#endif /* DEBUG */
cout << "\nInput string B (20 chars max!): ";
cin.getline(stringB, sizeof(stringB));
// Debug checks that stringB is input correctly. Positive.
#ifdef DEBUG
cout << "\n## String B: " << stringB[0] << stringB[1] << stringB[2] << stringB[3] << stringB[4] << stringB[5] << stringB[6] << stringB[7] << stringB[8] << stringB[9] << stringB[10] << stringB[11] << endl << endl;
#endif /* DEBUG */
/* The following for loop cycles through
* character array entries until either:
* a) it runs into two character which aren't equivalent,
* excluding the case where the character on string A is
* '/0'. It then reports to the user, stating that string
* A is not the beginning of string B.
*
* b) it runs into '/0' on string A in which case
* it assumes that since all previous comparisons were
* non-conflictual, string A is indeed the beginning of
* string B.
*/
for(int index = 0; /*nothing*/ ; ++index) {
// Debug checks that the right letters are being compared. Positive.
#ifdef DEBUG
cout << "\n##stringA[" << index << "]: " << stringA[index] << endl;
cout << "\n##stringB[" << index << "]: " << stringB[index] << endl;
#endif /* DEBUG */
// checking case a)
if((stringA[index] != stringB[index]) && (stringA[index] != '/0'))
{ cout << "string A does not begin string B.\n\n";
break; }
// checking case b)
if(stringA[index] == '/0') {
cout << "string A begins string B.\n\n";
break; }
}
return 0; }
--- Program End ---
Since I'm trying to tackle the areas I have difficulty, I picked out some 'trickier' (emphasizing the noobishness on my part) exercises from an O'Reilly manual. They were all going well until this one, which is leaving me stumped. I'm supposed to write a program that takes two strings as input and checks that the first one is the beginning of the second one.
I wrote the following program to do so, which isn't exactly working (always reports string A is NOT the beginning of string B), and at compile time I get the following list of warnings:
++++
stringbegin.cpp:65:69: warning: multi-character character constantstringbegin.cpp:70:30: warning: multi-character character constant
stringbegin.cpp: In function `int main()':
stringbegin.cpp:65: warning: comparison is always true due to limited range of data type
stringbegin.cpp:70: warning: comparison is always false due to limited range of data type
++++
I've included the source code of the program below, which I've commented rather profusively. I'd be ever so thankful if someone who knows what he or she is doing could take a look at this and tell me what the hell is going wrong? I'm assuming it's something dead simple and I'm just missing out on something.
Thanks in advance guys...
-- Edward.
--- Program begin ---
/************************************************
* String Begin! *
* ------------- *
* This program checks if stringA begins *
* string B or not. *
* It makes no use of the string.h header. *
* *
* Painstakingly hacked together by *
* Edward G///////////. *
************************************************/
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
//#define DEBUG
/* Debug commented for now, given it's only set up to check that strings are input correctly.*/
int main() {
char stringA[11]; // Two strings being compared.
char stringB[21]; // A is smaller than B. User is informed of this.
cout << "\nInput string A (10 chars max!): ";
cin.getline(stringA, sizeof(stringA)); //
// Debug checks that stringA is input correctly. Positive.
#ifdef DEBUG
cout << "\n## String A: " << stringA[0] << stringA[1] << stringA[2] << stringA[3] << stringA[4] << stringA[5] << stringA[6] << stringA[7] << stringA[8] << stringA[9] << stringA[10] << stringA[11] << endl;
#endif /* DEBUG */
cout << "\nInput string B (20 chars max!): ";
cin.getline(stringB, sizeof(stringB));
// Debug checks that stringB is input correctly. Positive.
#ifdef DEBUG
cout << "\n## String B: " << stringB[0] << stringB[1] << stringB[2] << stringB[3] << stringB[4] << stringB[5] << stringB[6] << stringB[7] << stringB[8] << stringB[9] << stringB[10] << stringB[11] << endl << endl;
#endif /* DEBUG */
/* The following for loop cycles through
* character array entries until either:
* a) it runs into two character which aren't equivalent,
* excluding the case where the character on string A is
* '/0'. It then reports to the user, stating that string
* A is not the beginning of string B.
*
* b) it runs into '/0' on string A in which case
* it assumes that since all previous comparisons were
* non-conflictual, string A is indeed the beginning of
* string B.
*/
for(int index = 0; /*nothing*/ ; ++index) {
// Debug checks that the right letters are being compared. Positive.
#ifdef DEBUG
cout << "\n##stringA[" << index << "]: " << stringA[index] << endl;
cout << "\n##stringB[" << index << "]: " << stringB[index] << endl;
#endif /* DEBUG */
// checking case a)
if((stringA[index] != stringB[index]) && (stringA[index] != '/0'))
{ cout << "string A does not begin string B.\n\n";
break; }
// checking case b)
if(stringA[index] == '/0') {
cout << "string A begins string B.\n\n";
break; }
}
return 0; }
--- Program End ---