2

I wrote this function to read a char array from cin:

#include <iostream>
#include <stdlib.h>
#include <cstring>
#include "main.h"
int read (char* buffer, int size) {

//read from standart input (with whitespaces)
//cin >> buffer;
cin.get(buffer, size);
cin.ignore(1, '\n');
cout << "cin get buffer: " << buffer << endl;

//if not a correct input
if (!cin.good())
    return 0;

cout << "cin.good: " << cin.good() << endl;

//user wants to quit
if (!strcmp (buffer, "end"))
    return 0;

return 1;
}

When I call this function for the first time in my main returncode = read (first, MAX) and enter blabla, it reads "blabla" into the buffer. (I checked via cout-for-loop)

When I want to read another array (for comparison), and do the exact same thing returncode = read(second, MAX), it only reads " labla" where second[0] remains empty.

Where is my fault? Feel free to ask for the rest of the code, but I think the fault is within this code snippet.

Thank you in advance!

ps: I am really new to c++, so please be patient with me :) pps: This is a university test, we are not allowed to use the string class..

EDIT: A simple main to test the above

main.cpp:

#include <iostream>
#include "main.h"
#define MAX 200
using namespace std;

int main () {
   char first [MAX] = {0};
   char second [MAX] = {0}; 

   cout<<"Please enter the first string to compare: "<<endl;
   returncode = read (first, MAX);
   cout<<"Please enter the second string to compare: "<<endl;
   returncode = read (second, MAX);

    switch (strcmp_ign_ws(first, second, MAX)) {
    case EQUAL:
       cout << "Strings are equal!" << endl;
       break;
    case SMALLER:
       cout << "String 1 is lexically smaller!" << endl;
       break;
    case BIGGER:
       cout << "String 1 ist lexically bigger!" << endl;
       break;
   }
}

and the main.h:

#define EQUAL 0
#define SMALLER -1
#define BIGGER 1

int read (char*, int);
int strcmp_ign_ws (char*, char*, int);
int main ();

EDIT 2: adding the String compare ignore whitespace function

As the error does not seem to be in the read function, this is the file using the two inputed-buffers first and second:

#include <stdlib.h>
#include <stdio.h>
#include <cstring>
#include <iostream>
#include <ctype.h>
#include "main.h"

using namespace std;

char * rm_ws (char * buffer, const int size) {

    int i,j;
    char *output=buffer;
    for (i = 0, j = 0; i<size; i++,j++)
    {
        if (buffer[i]!=' ')
            output[j]=buffer[i];
        else
            j--;
    }
    output[j]=0;
    return output;

}


int strcmp_ign_ws (char * first, char * second, int size) {
    first = rm_ws(first, size);
    second = rm_ws(second, size);


    if (strcmp (first, second) == 0)
        return EQUAL;
    if (strcmp (first, second) < 0)
        return SMALLER;
    if (strcmp (first, second) > 0)
         return BIGGER;

    }
}

ps: the rm_ws function is from stackoverflow already

Community
  • 1
  • 1
Dominik Reinert
  • 1,075
  • 3
  • 12
  • 26
  • 1
    Can you please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve), including a simple `main` function that calls the function you show. Also include the actual (and expected) output from the program. – Some programmer dude Nov 30 '15 at 08:51
  • I edit my post. One second pls. – Dominik Reinert Nov 30 '15 at 08:52
  • You *do* input something for the second call? – Some programmer dude Nov 30 '15 at 09:04
  • I call this program via command shell. It needs User input, so yes, I am sure I input something. When doing something like `for (int i = 0; i < size; i++){ cout << first [i] << endl; cout << second[i] << endl;}` I can see, that I inputed something. It works fine for the first call, but in the second call it throws away the **first** char – Dominik Reinert Nov 30 '15 at 09:08
  • 1
    Well for starters your loop is wrong, it should not use `i < size` as condition, but rather `first[i] != '\0'` – Some programmer dude Nov 30 '15 at 09:11
  • 1
    No repro with GCC or MSVC after fixing a minor problem (that perhaps indicates the posted code isn't the code the OP is actually running?). – Michael Burr Nov 30 '15 at 09:12
  • Okay, thank you for this, but that cannot be the reason why it is not reading the input correct as this was for debugging only.. – Dominik Reinert Nov 30 '15 at 09:13
  • @MichaelBurr this is correct, it is a simplyfied version. The whole program comes with 6 files, is written in german and only the three files I posted are closely connected to the input fault – Dominik Reinert Nov 30 '15 at 09:14
  • Regarding the posted code - the important thing isn't so much whether it has been edited down from your larger actual program, the important thing is whether or not the code you post produces the problem for you. It doesn't for me (and presumably at least one other person who voted up the comment). Are you sure that you see the problem with the code you posted here? if so, details on the platform and tools being used might be helpful. – Michael Burr Nov 30 '15 at 09:21
  • By the way, I can't replicate it either: http://ideone.com/tyUNr0 – Some programmer dude Nov 30 '15 at 09:24
  • I added the rest of the code. the remaining file, that I have not posted now is not used anymore - I used this forum discussion as a way to refactor ;) – Dominik Reinert Nov 30 '15 at 09:37

2 Answers2

0

Are you calling this on Windows or a Unix based system? On Unix it doesn't have that problem, so maybe it's related to the different line ending in Windows ('\r\n' vs. '\n') and you using

cin.ignore(1, '\n');

Not sure if that's the problem, but just try and replace that line with a simple

cin.ignore();
ChatterOne
  • 3,381
  • 1
  • 18
  • 24
0

After hours of debugging I finally came to the answer:

In my rm_ws function was a little mistake:

Instead of output[j] = 0 it needs to be output[j-1] = 0, just before the return statement.

Thank you for you help though!

Dominik Reinert
  • 1,075
  • 3
  • 12
  • 26
  • There's no problem with `rm_ws()`. Your problem is elsewhere. Please provide a short and complete example code that reproduces the issue. – P.P Dec 04 '15 at 13:03
  • Thank you for your effort, but the issue is resolved already and as mentioned in the second edit, this IS the whole code I am running – Dominik Reinert Dec 04 '15 at 14:02