Reputation: 335
I am trying to reverse words of each sentence using std::stack
from the c++ standard library.
The input file contents are:
3
foobar
this is a test
all your base
So the answer should be:
foobar
test a is this
base your all
But instead, the answer is:
foobar
test test test test
base base base base
I cannot figure out why. Following is the code:
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <cstring>
#include <stack>
using namespace std;
int main() {
FILE *fp, *fpo;
fp = fopen("test.in", "r");
int tests;
fscanf(fp, "%d", &tests);
char ch;
fscanf(fp, "%c", &ch);
for (int i = 0; i < tests; i++) {
char *letters;
letters = (char*)calloc(1000, sizeof(char));
stack <char*> words;
fscanf(fp, "%s", letters); fscanf(fp, "%c", &ch);
while (ch != '\n' && ch != EOF) {
words.push(letters); printf(" %s", words.top());
fscanf(fp, "%s", letters); fscanf(fp, "%c", &ch);
}
words.push(letters); printf(" %s", words.top());
printf(" -- ");
while (!words.empty()) {
printf(" %s", words.top());
words.pop();
}
printf("\n");
free(letters);
}
fclose(fp);
}
Upvotes: 0
Views: 589
Reputation: 148900
As others say in comments, your code is bad:
char *
in it, when you could use std::string
std::stringstream
would help here)fscanf
instead of fgets
(or better getline
as your question is tagged c++)But the actual cause of your error is simply that you are storing a char *
in your stack, that always point to the same char array, instead of allocating a new array for each word. So:
and your stack contains n copies of the address of letters each pointing to the same array of last word.
You should either:
std::string
, and better a stack<string>
(the C++ way) to let the C++ library manage allocation and deallocation for you.There can be good reasons to use C library functions in a C++ program (size or performance constraints in a program that already uses C code, assignement to do so), but unless it is the case, the high level C++ library is simpler to use with less risk of errors than the low level C one.
TL/DR: If you had used C++ library (getline, std::string, iostream, stringstream, stack) the library itself would have saved you from that error.
Upvotes: 1
Reputation: 7996
Please don't mix C and C++ this way. It is almost unreadable. Some guidelines:
std::string
to extract each word from the line.Upvotes: 4