Jump to content

More C++ Problems

Go to solution Solved by CookieMaster,

Thanks all that helped, I have finished and tested and all works fine. 

What's the error you're getting? I haven't used C++ myself, though I have used Java quite a bit (They're similar), so I might be able to help.

Specs: CPU - Intel i7 8700K @ 5GHz | GPU - Gigabyte GTX 970 G1 Gaming | Motherboard - ASUS Strix Z370-G WIFI AC | RAM - XPG Gammix DDR4-3000MHz 32GB (2x16GB) | Main Drive - Samsung 850 Evo 500GB M.2 | Other Drives - 7TB/3 Drives | CPU Cooler - Corsair H100i Pro | Case - Fractal Design Define C Mini TG | Power Supply - EVGA G3 850W

Link to comment
https://linustechtips.com/topic/749462-more-c-problems/#findComment-9488149
Share on other sites

Link to post
Share on other sites

Just now, TheKDub said:

What's the error you're getting? I haven't used C++ myself, though I have used Java quite a bit (They're similar), so I might be able to help.

expected a statement. I have a feeling I know what the problem is and maybe where but I just can't think :/

Link to comment
https://linustechtips.com/topic/749462-more-c-problems/#findComment-9488155
Share on other sites

Link to post
Share on other sites

14 minutes ago, CookieMaster said:

Thanks, that fixed it, I knew i was missing something like that.

The {} brackets really help with organization. Your indentation was off because you didn't have the brackets. Therefor, the compiler was looking at your else if statements as part of the original conditional statement.

Good luck on the rest of it!

Link to comment
https://linustechtips.com/topic/749462-more-c-problems/#findComment-9488247
Share on other sites

Link to post
Share on other sites

I recommend for any programming language that normally requires curly braces to create a new scope, use it even if it allows things like omitting them for single line if-statements, for-loops, or while-loops. Mostly so you don't run into this issue again.

 

If people get annoyed by

if (something)
{
	//Do something that requires one line
}

They have nothing better to do with their time :B

Link to comment
https://linustechtips.com/topic/749462-more-c-problems/#findComment-9488284
Share on other sites

Link to post
Share on other sites

43 minutes ago, CookieMaster said:

Yeah I feel stupid asking, but still new to C++, what am I missing and where. Thanks (the error is on both else's)

 

 


if (choice == 1)
	cout << "Enter a file name." << endl; // Have end user input a file name
	cin >> filename;
	ofstream outFile((filename + extension).c_str()); // Get the file name and add extionsion to it.
	outFile.open(filename);
	 else if (choice == 2)
		cout << "It appears the programmer has not coded this part in" << endl;
	 else if (choice == 4)
		cout << "Hello" << endl;

 

Also note that ofstream's constructor will open the file so the extra call to open is redundant. Since C++11 the constructor also accepts a std::string reference, which is the preferred choice.

 

Link to comment
https://linustechtips.com/topic/749462-more-c-problems/#findComment-9488347
Share on other sites

Link to post
Share on other sites

32 minutes ago, M.Yurizaki said:

I recommend for any programming language that normally requires curly braces to create a new scope, use it even if it allows things like omitting them for single line if-statements, for-loops, or while-loops. Mostly so you don't run into this issue again.

 

If people get annoyed by


if (something)
{
	//Do something that requires one line
}

They have nothing better to do with their time :B

Thanks

16 minutes ago, Unimportant said:

Also note that ofstream's constructor will open the file so the extra call to open is redundant. Since C++11 the constructor also accepts a std::string reference, which is the preferred choice.

 

Thank you for letting me know.

 

And now I have a diffrent problem in which I'm not understanding. On lines 28 and 42 its saying
Error    C3867    'std::basic_ifstream<char,std::char_traits<char>>::is_open': non-standard syntax; use '&' to create a pointer to member    (replace ifstream with ofstream for other error)

 

Here's my code now 

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	int choice;
	string filename;
	string type;
	
	cout << "*****Welcome To PACS!*****" << endl << "Choose An Option:" << endl; // The basic display of the program.
	cout << "[1] C-STORE" << endl;
	cout << "[2] C-FIND" << endl;
	cout << "[3] C-GET" << endl;
	cout << "[4] C-ECHO" << endl;
	cout << "[5] Exit" << endl;
	cout << "Enter choice [1-5]:" << endl;
	cin >> choice; // Get user input into an int.
	// The if statements to make the program work.
	if (choice == 1)
	{
		cout << "Enter a file name." << endl; // User needs to put a file name to search.
		cout << "Enter a file type.(Must have a . in the front" << endl; // Have user put in type.
		cin >> type;
		ofstream outFile((filename + type).c_str()); // Get the file name and add extension to it.
		if (outFile.is_open) 
		outFile.close();
	}
	else if (choice == 2)
	{
		cout << "It appears the programmer has not coded this part in." << endl;
	}
	else if (choice == 3)
	{
		cout << "Enter a file name." << endl; // User needs to put a file name to search.
		cin >> filename;
		cout << "Enter a file type.(Must have a . in the front." << endl; // Have user put in type.
		cin >> type;
		ifstream File((filename + type).c_str()); // Get the file name and add extension to it.
		if (File.is_open)
			getline(File, type);
		File.close();
	}
	else if (choice == 4)
	{
		cout << "Hello" << endl;
	}
	
    return 0;
}

 

Link to comment
https://linustechtips.com/topic/749462-more-c-problems/#findComment-9488434
Share on other sites

Link to post
Share on other sites

Oh, I also thought of a practical reason why to add curly braces your if-statements and loops all the time: if you need to expand it, you don't have to remember to add the curly braces.

Link to comment
https://linustechtips.com/topic/749462-more-c-problems/#findComment-9488455
Share on other sites

Link to post
Share on other sites

19 minutes ago, CookieMaster said:

And now I have a diffrent problem in which I'm not understanding. On lines 28 and 42 its saying

Error    C3867    'std::basic_ifstream<char,std::char_traits<char>>::is_open': non-standard syntax; use '&' to create a pointer to member    (replace ifstream with ofstream for other error)

 

Here's my code now 


#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	int choice;
	string filename;
	string type;
	
	cout << "*****Welcome To PACS!*****" << endl << "Choose An Option:" << endl; // The basic display of the program.
	cout << "[1] C-STORE" << endl;
	cout << "[2] C-FIND" << endl;
	cout << "[3] C-GET" << endl;
	cout << "[4] C-ECHO" << endl;
	cout << "[5] Exit" << endl;
	cout << "Enter choice [1-5]:" << endl;
	cin >> choice; // Get user input into an int.
	// The if statements to make the program work.
	if (choice == 1)
	{
		cout << "Enter a file name." << endl; // User needs to put a file name to search.
		cout << "Enter a file type.(Must have a . in the front" << endl; // Have user put in type.
		cin >> type;
		ofstream outFile((filename + type).c_str()); // Get the file name and add extension to it.
		if (outFile.is_open) 
		outFile.close();
	}
	else if (choice == 2)
	{
		cout << "It appears the programmer has not coded this part in." << endl;
	}
	else if (choice == 3)
	{
		cout << "Enter a file name." << endl; // User needs to put a file name to search.
		cin >> filename;
		cout << "Enter a file type.(Must have a . in the front." << endl; // Have user put in type.
		cin >> type;
		ifstream File((filename + type).c_str()); // Get the file name and add extension to it.
		if (File.is_open)
			getline(File, type);
		File.close();
	}
	else if (choice == 4)
	{
		cout << "Hello" << endl;
	}
	
    return 0;
}

 

is_open is a member function, you need to add () to function calls:

if (outFile.is_open())

There's also no need to manually call close. When the ofstream object goes out of scope it's destructor will close the file automatically.

Link to comment
https://linustechtips.com/topic/749462-more-c-problems/#findComment-9488521
Share on other sites

Link to post
Share on other sites

8 minutes ago, Unimportant said:

is_open is a member function, you need to add () to function calls:


if (outFile.is_open())

There's also no need to manually call close. When the ofstream object goes out of scope it's destructor will close the file automatically.

Thanks I removed that, and that's what I thought the problem was. Silly me kept putting )()). Plays with your eyes a bit I guess. 

Link to comment
https://linustechtips.com/topic/749462-more-c-problems/#findComment-9488557
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×