ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
A few days playing with c++. I,m having trouble with else if,
And a few queries with my program, If any 1 can help with. The program runs and dose what I want
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream myFile("/home/spiky/Exercise1/test1/Doc1.txt"); //Do i have to make an entry for each file
ifstream myFile2("/home/spiky/Exercise1/test1/Doc2.txt"); //Otherwise how?
string line;
char Answer;
cout << "Which file do you want to open(1=Doc1/2=Doc2)? ";
cin >> Answer;
if( Answer == '1' ) // Doc1
if (myFile)
{
while (getline(myFile, line))
{
cout << line << endl;
}
}
else // Tried else if here with the 2nd part
cerr << "Failed to open Doc1." << endl;
if( Answer == '2' ) // Doc2
if (myFile2)
{
while (getline(myFile2, line))
{
cout << line <<endl;
}
}
else
cerr << "Failed to open Doc2." << endl;
return 0;
}
I put the comments to try to show what I had tried
Click here to see the post LQ members have rated as the most helpful post in this thread.
Sorry, but that is NOT true. The operator bool() method for the stream object will be called; it returns the same value as if the good() method had been called.
Hi
The 1st program works ok (there is a problem, It puts a ? in a diamond on the command line). But I would like to know where I,m wrong on the 2nd program.
I have googled but all i can find are using cout to output something, not open a Doc.
I know this is all beginner stuff but thats what I am,
dwhitney67 I have tried to put in to practice from your last post but cant seem to make it work.
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream myFile("/home/spiky/Exercise1/test1/Doc1.txt" , ifstream::out ); //Do i have to make an entry for each file
ifstream myFile2("/home/spiky/Exercise1/test1/Doc2.txt" , ifstream::in ); //Otherwise how?
string line;
char Answer;
cout << "Which file do you want to open(1=Doc1/2=Doc2)? ";
cin >> Answer;
if( Answer == '1' ) // Doc1
// if (myFile)
{
while (myFile.good ())
{
cout << (char) myFile.get();
}
myFile.close ();
}
// else
// cerr << "Failed to open Doc1." << endl;
else if( Answer == '2' ) // Doc2
if (myFile2)
{
while (getline(myFile2, line))
{
cout << line <<endl;
}
}
else
cerr << "Failed to open Doc." << endl;
return 0;
}
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream MyFile("home/spiky/Exercise1/test1/Doc1.txt");
ifstream MyFile2("~/Exercise1/test1/Doc2.txt");
ifstream MyFile3("~/Exercise1/test1/Doc2.txt");
string line;
char Answer;
cout << "Which Document do you want to view(1=Doc1/2=Doc2/3=Doc3) ";
cin >> Answer;
if ( Answer == '1' )
{
if (MyFile)
{
while (getline (MyFile, line))
{
cout << line << endl;
}
}
else
{
cerr << "Failed to open Doc1" << endl;
}
{
else if( Answer == '2' )
}
if (MyFile2)
{
{
while (getline (MyFile2, line ))
{
cout << line << endl;
}
}
}
else
{
cerr << "Failed to open Doc2." << endl;
}
cin.get();
}
Hi
The 1st program works ok (there is a problem, It puts a ? in a diamond on the command line).
You should know that myfile.good() will only return false after you try to read beyond the end of the file. You read and write a character after the end of the file was reached.
As for the second program, your braces are still a mess. You should learn to keep your indentation consistent troughout your code. Then it will be easier for you to spot the errors in your syntax.
Code:
{
else if( Answer == '2' )
}
this is a conditional statement without a body enclosed in braces. There should actually be a closing brace there:
Code:
} else if( Answer == '2' )
Also, be careful about ambiguous elses.
if you have something like:
Code:
if(a)
if(b) {
...
}
else { ... }
I would recommend to allways put explicit braces here. Either:
Ok I have tried looking up indenting the code and that shows various examples, they basically say keep it all the same uniformed
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream MyFile("home/spiky/Exercise1/test1/Doc1.txt");
ifstream MyFile2("~/Exercise1/test1/Doc2.txt");
ifstream MyFile3("~/Exercise1/test1/Doc2.txt");
string line;
char Answer;
cout << "Which Document do you want to view(1=Doc1/2=Doc2/3=Doc3) ";
cin >> Answer;
if ( Answer == '1' ){
if (MyFile)
{
while (getline (MyFile, line))
{
cout << line << endl;
}
}
else
{
cerr << "Failed to open Doc1" << endl;
}
} else if( Answer == '2' )
if (MyFile2)
{
while (getline (MyFile2, line ))
{
cout << line << endl;
}
}
else
{
cerr << "Failed to open Doc2." << endl;
}
cin.get();
}
I also cant get my head around why it wont work! It produces the cerr, but wont show the file Doc1 or doc2, where the 1st example runs and shows both Docs as asked for.
I also hope that the Layout is better
My first question... what editor are you using? Your perception of good indentation is not evident after you posted your code. Did you review the code you posted? The indentation is crap.
Look at this:
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
char Answer;
cout << "Which Document do you want to view(1=Doc1/2=Doc2/3=Doc3) ";
cin >> Answer;
if ( Answer == '1' )
{
ifstream MyFile("Doc1.txt");
if (MyFile)
{
string line;
while (getline(MyFile, line))
{
cout << line << endl;
}
}
else
{
cerr << "Failed to open Doc1" << endl;
}
}
else if( Answer == '2')
{
ifstream MyFile("Doc2.txt");
if (MyFile)
{
string line;
while (getline(MyFile, line ))
{
cout << line << endl;
}
}
else
{
cerr << "Failed to open Doc2." << endl;
}
}
else if( Answer == '3')
{
ifstream MyFile("Doc3.txt");
if (MyFile)
{
string line;
while (getline(MyFile, line ))
{
cout << line << endl;
}
}
else
{
cerr << "Failed to open Doc3." << endl;
}
}
else
{
cerr << "Bad input." << endl;
}
}
When you see repetitive code, such as shown above, this is a strong clue to refactor the code to use a function to encapsulate the common code (such as to open/read a file). I do not know if you have yet covered functions in your studies, but it is something that I recommend you do today. Also, look into replacing many if-else statements with the use of a switch construct.
Last edited by dwhitney67; 05-13-2012 at 07:01 AM.
I realise this is alot harder than I thought, I picked up on doing this a week ago as a pure past time. I googled and picked up on a few tutorials which work, I thought I could expand on them. (Experimental) Not realiseing things don't quite work as expected.
I did know there would be alot of studying
I,m not trying to rewrite an OS just keep myself amused
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.