char string problem
This is a discussion on char string problem within the C++ Programming forums, part of the General Programming Boards category; Hi!
Please check out the code below.
Code:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
void main()
{
char input[20]="
";
cout<<"English ...
-
char string problem
Hi!
Please check out the code below.
Code:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
void main()
{
char input[20]="\0\0";
cout<<"English word?: ";
cin>>input;
ifstream fs;
char let[2]="\0";
char wrd[20]="\0\0";
int i=0;
fs.open("lib.txt", ios::in);
while(fs.peek()!='.') //check word end
{
fs.read(let,1);
wrd[i] = let[0]; //create word
i++;
}
if(input==wrd)
{
cout<<wrd;
}
} The problem is that the if-statement doesn’t detect it when input and wrd
seems to contains the same string. Please show me my fault.
Thanks in advance.
-geek@02
-
ATH0

Originally Posted by
geek@02 Hi!
Please check out the code below.
Code:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
void main()
{
char input[20]="\0\0";
cout<<"English word?: ";
cin>>input;
ifstream fs;
char let[2]="\0";
char wrd[20]="\0\0";
int i=0;
fs.open("lib.txt", ios::in);
while(fs.peek()!='.') //check word end
{
fs.read(let,1);
wrd[i] = let[0]; //create word
i++;
}
if(input==wrd)
{
cout<<wrd;
}
} The problem is that the if-statement doesn’t detect it when input and wrd
seems to contains the same string. Please show me my fault.
Thanks in advance.
-geek@02
1) This is wrong. Read the FAQ.
2) You cannot compare character arrays with the == operator. You'll have to use something like strcmp.
Quzah.
Hope is the first step on the road to disappointment.
-
Cat without Hat
Better yet, since you already include the <string> header anyway, you should use the std::string class.
All the buzzt!

CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
-
Thanks for that.
Yet another problem cropped up. I changed the above code a bit:
Code:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
void main()
{
char input[20]="\0";
cout<<"English word: ";
cin>>input;
ifstream fs;
char let='\0';
char wrd[20]="\0";
int i=0;
fs.open("lib.txt", ios::in);
while(!fs.eof())
{
while(fs.peek()!=':') //check word end
{
let = fs.get();
wrd[i] = let; //create word
i++;
}
if(!strcmp(input, wrd))
{
cout<<wrd;
}
fs.seekg(1);
i=0;
for(int k=0; k<=20; k++)
{
wrd[k]='\0';
}
}
} Everything works fine during the 1st go of the outer while loop. But in the 2nd time looping, after get() is executed, the file pointer seems to jump 2 positions backwards. Why is this happening please?
-
Registered User

Originally Posted by
geek@02 Everything works fine during the 1st go of the outer while loop. But in the 2nd time looping, after get() is executed, the file pointer seems to jump 2 positions backwards. Why is this happening please?
Code:
while(!fs.eof())
{
while(fs.peek()!=':') //check word end
{
let = fs.get();
wrd[i] = let; //create word
i++;
}
if(!strcmp(input, wrd))
{
cout<<wrd;
}
fs.seekg(1);
i=0;
for(int k=0; k<=20; k++)
{
wrd[k]='\0';
}
} Just a guess, but... because you told it to (see code highlighted in red above). Of course it would help to see what your input file looks like to verify that.

I used to be an adventurer like you... then I took an arrow to the knee.
-
You should never loop on eof(), if you want to read untill you run out of things to read you loop untill you cannot read anymore, then check eof() to see if the reason you stopped was because you ran out of things to read. My guess is that you are using seekg to skip a position, and as hk_mp5kpdw pointed out you are actually moving to the second char in the file.
fortunately if you use std::string this becomes a good bit easer, I think this is what you are trying to do Code:
int main() {
using namespace std;
cout << "Enter a word to search for in file\n:";
string input;
if(cin >> input) {
ifstream fs("lib.txt");
int wordcount=0;
string word;
while(getline(fs,word,':')) {
if(input == word) {
cout << "word #" << wordcount << " == " << word << endl;
}
++wordcount;
}
} else {
cerr << "standard input failure, giving up" << endl;
return 2;
}
return 0;
} This version requres that there be no whitespace in lib.txt, and is case sensitive. Input ignores whitespace. Thus if lib.txt contains "pear: apple:grape" and the user enters " apple" input will be equal to "apple" and word will be " apple".
getline consumes it's terminator. The string returned does not contain the terminator. If the file ends before a terminator, but characters were read then eof() is true but so is good() If you loop on eof() you will exit before handling trailing characters. If the final character is a terminator then eof() is false and you will expect more data where none exists.
-
Yes I’m using seekg() to skip ‘:’. Now I replaced it with another get().
Thanks for the eof() explanation and all, guys.
Popular pages Recent additions
Similar Threads
-
By Neolyth in forum C Programming
Replies: 16
Last Post: 06-21-2009, 04:05 AM
-
By the_newbug in forum C Programming
Replies: 4
Last Post: 03-03-2006, 01:11 AM
-
By bennyandthejets in forum Game Programming
Replies: 29
Last Post: 08-25-2003, 11:58 AM
-
By OmniMirror in forum C Programming
Replies: 4
Last Post: 05-14-2003, 09:40 PM
-
By muffin in forum C Programming
Replies: 0
Last Post: 08-24-2001, 10:13 AM