Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

C++ woes: something more specific for you all

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 ---

Name: abez !XWEgiX8ArQ 2005-06-23 23:51

    cin.getline(stringA, sizeof(stringA));

You know that sizeof refers to the storage to hold something of that type. The type of stringA is char * which is a pointer. So that'd be 4 or 8.
The size of your string is 11, leaving that you want 10 characters.
C and C++ do not know how long arrays are. You have to remember. If you are having too much trouble make a struct.
If the string is input correctly you can just go:
cout << "\n SO GAY DICKS IN MY MOTUH: " << stringA;

also

it is \0 not /0, that's what the warstringbegin.cpp
:65:69: warning: multi-character character constantstringbegin.cpp
is about.

When you are given a warning go and look at the line number and see what it is whining about.

Name: madleser !r4YvKJpWUc 2005-06-24 6:01

The type of stringA is char * which is a pointer. So that'd be 4 or 8.
'char stringA[11]' is an array, so sizeof returns the space allocated by it, namely 11 bytes. if you were to write 'char* stringA' and malloc'd/new'd space, *then* you'd have to remember how much you allocated.

>>1
replace all occurences of "/0" with "\0" and it will work.

btw:
instead of
cout << "\n## String A: " << stringA[0] ... << stringA[n] << endl;
you can just write
cout << "\n## String A: " << stringA << endl;
.

instead of
if ((stringA[index] != stringB[index]) && (stringA[index] != '\0'))
you can write
if (stringA[index] && (stringA[index] != stringB[index]))
since if stringA[index] is '\0', it's automatically false. checking for that one first also saves the program the trouble of comparing the strings and then noticing that it doesn't matter anyway if it's at the end of stringA.

Name: Edward 2005-06-27 18:07

>>3
Thanks for not only fixing the program, but also for the efficiency tip. This is both reassuring because it means my logic wasn't too bad, and saddening because I made such a crappy mistake. But hey, mistakes make us better learners...

Name: Anonymous 2005-06-27 20:46

if you put your logic into a function you don't need fancy flow control (break), you can just use return

Name: Edward 2005-06-28 6:20

>>5
I see. I'll keep that in mind for more complex programs (where I'd be more likely to divide everything into functions anyway). In a program such as this, where the function is only called once, would it be a wise thing to inline it?

Name: madleser !r4YvKJpWUc 2005-06-28 8:41

i reckon that it will make no difference. "inline" replaces every call to that function with the function itself, so i'd avoid it unless the function is invoked in short intervals and you can't afford the call-overhead.

Name: Anonymous 2005-06-29 7:10

>>5
Agreed for units of work, or long processes. Disagreed for short loops that belong to the function they are in, yet break or continue are a good idea.

Name: Anonymous 2005-06-29 7:12

>>6
If the function is merely a separator as it's called only once, or it's short enough, or some special circumstances are given, don't worry - good compilers (GCC, Intel, ...) will make it inline if you compile with optimizations (always should) and without debug (final version should) - the function will completely disappear and the code won't even jump anywhere. Verified with a disassembler.

Name: Anonymous 2005-06-29 18:13

Premature optimisation is the root of all evil.

Name: Anonymous 2005-06-29 18:14

Premature optimisation is the root of all evil.

Name: Anonymous 2005-06-29 22:18

Premature optimisation is the root of all evil. <--- Game Programmers are the dumbest people on earth (this isn't a quote you'll get from a Game Programmer)

Name: Anonymous 2005-06-30 2:10

I'd pit Carmack against anyone anyday.

Name: Anonymous 2005-06-30 3:19

C++ sucks, use C

Name: Anonymous 2005-06-30 3:20

Lol Carmack is a very exceptional game programmer, he used ALGORITHMS to make his games work fast, unlike you faggots who think that preincrement will make your game run faster than postincrement.

Name: Anonymous 2005-06-30 4:01 (sage)

If someone honestly thinks that pre/postincrement will yield a significant speed difference they don't deserve to call themselves a game programmer, asshat.

ps. modern games are almost always video hardware limited, speed-wise.

Name: 13 2005-06-30 4:32 (sage)

What >>16 said.

Do you really think a game programmer doesn't think of algorithmic complexity and similar issues? Who do you hang out with anyway? 14 year old "game programmers" banging out code in VB?

Name: Anonymous 2005-06-30 7:45

>>14
FTW

>>16
FTW

John Carmack: Mister Hype.

Optimizing early: Wise. No need to write shit, then have to fix it. And you're not writing before design is moderately complete, right?

Optimization: Only noobs think operator usage is all there is to optimization.

Game programmers: Two types: the ones making money from it, usually serious, but usually careless about optimization (unfortunately), and VB/Java kids.

Name: Anonymous 2005-07-15 14:14

/* That's the ugliest POS I've ever written but I don't care because I'm a student! */
#include <stdio.h>

/* We all love C++ */
typedef enum { false, true } bool;

/* Checks if a begins with b */
bool beginsWith(char *a, char *b)
{
    char ca, cb;
   
    if ((! a) || (! b)) {
        return false;
    }
   
    while ((ca = *a) && (cb = *b)) {
        if (ca != cb) {
            return false;
        }
       
        a++;
        b++;
    }
   
    if (! *b) {
        return true;
    }
   
    return false;
}

int main (int argc, char **argv)
{
    if (argc != 3) {
        return 1;
    }
   
    printf(beginsWith(argv[1], argv[2]) == true ? "true\n" : "false\n");
   
    return 0;
}

Name: Anonymous 2005-07-16 2:48

Suggestions if you're really into C++:
- substitute <stdio.h> with <stdio>
- use a namespace
- get rid of printf (mind you, iostream sucks)
- too many returns in beginsWith()

Name: Anonymous 2005-07-16 5:24

<cstdio>

Name: Anonymous 2007-08-04 14:15 ID:60dgxtR7

poop

Name: Anonymous 2008-03-24 6:49

>>22
I concur.

Name: Anonymous 2008-03-24 6:58

DON'T HELP HIM!!!

Name: Anonymous 2008-03-24 7:25

typedef enum { false, true } bool;
ugh. 1 and 0 too easy for you?

Name: Anonymous 2008-03-24 7:33

Edward G, your name rhymes with faggot tree.

Name: Anonymous 2008-03-24 11:38


typedef enum { zero, one, two, three, four, five, six, seven, eight, nine, ten } num;

Name: Anonymous 2008-03-24 19:18

>>27
typedef enum { zero, one, two, three, four, five, six, seven, eight, nine, ten, ... twobilliononehundredfourtysevenmillionfourhundredeightythreethousandsixhundredfourtyseven } number;

Name: Anonymous 2008-03-24 19:21

>>28
Did you steal that from Sussman's MIT Scheme codebase?

Name: Anonymous 2008-03-24 19:39

>>29
It's not stealing if it's OPEN SOURCEE.

Name: Anonymous 2008-03-24 19:43

>>30
I don't buy this until you are backed up by Eben Moglen.

Name: Anonymous 2008-03-25 1:20

>>28

You doesn't put 'and's between your hundreds?

one hundred and fifty eight

Name: Anonymous 2009-03-06 5:42

The project No kidding?

Name: Anonymous 2010-07-20 5:28

[b][i][u]sage[/b][/i][/u]

Name: Anonymous 2010-07-20 5:29

sage

Name: Anonymous 2010-07-20 5:54

>>34-35
Why the fuck would you bump this horrible thread.

Name: Anonymous 2010-12-23 22:31

<

Name: Anonymous 2011-02-02 23:48

Name: Anonymous 2011-02-04 15:06

Name: Anonymous 2011-03-02 21:33

I AM THE MOST FUCKING NGGER MATURE

Name: Anonymous 2013-07-29 9:15

check em

Name: Anonymous 2013-07-29 9:50

>>41
didn't check em, soz bro

Don't change these.
Name: Email:
Entire Thread Thread List