Results 1 to 6 of 6
The below program is giving unpredictable and wrong output when I compile with g++ under Ubuntu OS , tried with the complier avilable with Ubuntu 8.04 and Ubuntu 10.04 LTS.
...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 09-15-2010 #1Just Joined!
- Join Date
- Sep 2010
- Posts
- 4
Uncertain output of C++ String Concatenation Program
The below program is giving unpredictable and wrong output when I compile with g++ under Ubuntu OS , tried with the complier avilable with Ubuntu 8.04 and Ubuntu 10.04 LTS.
But it works fine when complied and ran Redhat g++ 3.4.6
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main() {
vector<string> v;
ifstream in("Fillvector.cpp");
string line;
//fflush (stdin);
//fflush (stdout);
while(getline(in, line)){
v.push_back(line); // Add the line to the end
}
string bigstring = "Hello";
// Add line numbers:
for(int i = 0; i < v.size() ;i++){
bigstring = bigstring + v[i];
}
cout <<" Big String " << endl;
cout << bigstring <<endl;
} ///:~
Its very confusing and request for help.
- 09-15-2010 #2
I can't find the mistake. Are you sure the file is opened for reading without errors?
Debian GNU/Linux -- You know you want it.
- 09-15-2010 #3Linux Newbie
- Join Date
- Mar 2010
- Posts
- 152
What "unpredictable and wrong" output is it giving? And what do you expect the correct output is?
- 09-15-2010 #4Just Joined!
- Join Date
- Sep 2010
- Posts
- 4
The file opens with out any errors. In the code the input file "Fillvector.cpp" is northing but the file name of the same program.
The "unpredictable and wrong" output is as follows...
gokul@honda:~/TCPPExercierses$ ./Fillvector
Big String
} ///:~<< bigstring <<endl; ndl;;+){ne to the end
gokul@honda:~/TCPPExercierses$ vim Fillvector.cpp
The ideal output is contents of entire file which is achieved by displaying the concatenated string i.e bigstring..
When I comment bigstring = bigstring + v[i]; and insert the statement cout <<v[i]<<endl; entire file is displayed which means there is no problem with file opening.
Not sure if some kind of type casting is needed for v[i] in the statement bigstring = bigstring + v[i];
- 09-16-2010 #5
You should not need one. v[i] returns a string. And bigstring is a string as well.
It runs as expected on Debian Lenny and SID.
The behaviour you describe is very strange indeed.
Now try this on your Ubuntu system:
Code:#include <string> #include <iostream> using namespace std; int main() { string bigstring = "Hello"; string smallstring = " world"; bigstring = bigstring + smallstring; cout <<" Big String " << endl; cout << bigstring <<endl; } ///:~Debian GNU/Linux -- You know you want it.
- 09-17-2010 #6Just Joined!
- Join Date
- Sep 2010
- Posts
- 4
I think I have figured out what's the problem is. As suspected initially it is because of input file, The input file is in DOS format and because of that there is a problem with concatenation. Though displaying (cout) the contents of v[i] works fine, concatenation is not working.
I am aware bit not that clear about the format and behavior of DOS file in Linux environment.
Below is the info for DOS file as seen in the vim
""Fillvector1.cpp" [dos] 22L, 594C"
and for Linux file it is "Fillvector3.cpp" 22L, 409C
I just copied the DOS file into Ubuntu OS so the problem, but in Redhat I typed it in a new file.


Reply With Quote

