Code Checker!. Can my code my improvedfurthermore.

Before, I start constructing a code for my main assignment. I would like to get an advice on my following code. Is there anything I can improve on this code and also, if there is a mistake can someone point it out. Im just asking the user to type in the input and the output file and from that oint onwards, it will open both of those two files. Also is my main scope of the area ok?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
void openfile_makefile(ifstream &in,ofstream &out,char filename[],int size, char filename2[], int size2)
{
	cout<<"Enter Input File Name: ";
	for (int row=0;row<size;row++)
		cin>>filename;

	//Open Input Fileone
	in.open(filename);

	cout<<"\n\nEnter Output File Name: ";
	for (int column=0;column<size2;column++)
		cin>>filename2;

	//Open output file
	out.open(filename2);


	



}

int _tmain(int argc, _TCHAR* argv[])
{
	//Display the objective of program
	display();

	//Call the function to open the input and output files
	ofstream fout;
	ifstream fin;
	char filenumberone[15];
	char filenumbertwo[15];
	openfile_makefile(fin,fout,filenumberone,15,filenumbertwo,15);
	
As a C++ programmer, it's advised that you use std::strings instead of char arrays.
I don't see a closing brace anywhere for your main function (which should also return something by the way).
Then of course there's the use of using namespace std; in global scope, but whatever that advice is almost cliche and you can do whatever you want.

Yeah, the only REAL recommendation is using std::strings. You can cut down the number of parameters for your openfile_makefile() function by using strings, and it makes the input less of a hassle.
Last edited on
Understood you for the first part :) when your talking about cutting down the parameters, I'm a little unsure about that... What exactly should I pull out and why?and I'm not sure how adding strings would cut down my parameter list :(
My next chapter covers string, but why is strong prefered better method in my case? :)
closed account (Dy7SLyTq)
you dont need the sizes because strings have sizes "built" in to them. i wouldnt use using namespace std; its very bad practice and can lead to a number of clashes.
Ok I kind of understood you for that part, but suppose I onky want the textile to be 7 characters. Wouldn't c-string be a better method to use?
*text file- file
And now I'm a little lost.i saw a code in my book illustrating steing [14] how would this code work? I can't recall it and I think there is no such thing as a string array :( I Couod be wrong though
closed account (Dy7SLyTq)
no c-string wouldnt be better. why do you think so? and writing steing[14] creates an array of 14 cells for type whatever_type_steing_is. and of course you can have an array of strings. you can have an array of anything except keywords.
The reason I think c string would be better is because I won't be able to allow the user to enter up to certain amount. For instance, I onky want a student name up to 15 characters. Once it has reached to 15, it will not read the other characters if there more then 16.

In steing, from my knowledge as a beginner, string will read the entire sentence, though yes I can just use cin operator to get the first part of the sentence before white space is found and ignore the rest of character or use getline until a certain delimiter is found.
Last edited on
Sorry if I'm bugging you too much :D I have never seen an example of a steing array. How does it look like. For instance, the character array would be something like

1
2
3
char name[16]
For (int row=o;row<16; row++)
Cin>>name[row]
in this particular code, the user will keep writing in a character.

For string array. How would it detect when I'm on the second index of string. I know I didn't explain this really good, but the character array would know what the second index is of the cstring....once again, definately lacked in explaining this, but any example of a steing area would help :(
Alright DTS and xismn. I used the following approach you guys recommended me too do so.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void openfile_makefile(ifstream &in,ofstream &out,string filename, string filename2)
{
	cout<<"Enter Input File Name: ";
	
		cin>>filename;

	//Open Input Fileone
	in.open(filename.c_str());

	cout<<"\n\nEnter Output File Name: ";
	
		cin>>filename2;

	//Open output file
	out.open(filename2);


	



}



1
2
//Open Input Fileone
	in.open(filename.c_str());
I read in the book that if im declaring it as a string variable, then when I open the file, I need to convert that into a c-string. So would this be correct?
The reason I think c string would be better is because I won't be able to allow the user to enter up to certain amount. For instance, I onky want a student name up to 15 characters. Once it has reached to 15, it will not read the other characters if there more then 16.


Ignoring extraneous input is not a very good solution, whether you choose to use C style strings or C++ strings.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>

std::string get_some_text(const std::string& prompt, unsigned max_length)
{
    bool success = false;
    std::string result;

    while (!success)
    {
        std::cout << prompt << "\n> " ;
        std::cin >> std::ws;        // remove any leading whitespace
        std::getline(std::cin, result);

        if (result.length() <= max_length)
            success = true;
        else
        {
            std::cout << "Input was " << result.length() - max_length 
                      << " characters too long.\n";
        }
    }

    return result;
}

int main()
{
    std::string str = get_some_text("Enter some text no longer than 15 chars. ", 15);
    std::cout << "You entered \"" << str << "\"\n";
}
Last edited on
I will keep that in mind cire. Meanwhile, for this code i understood every part of this code except lines 11-13. Promt is a reference parameter, but you have it as cout..prompt. Furthermore, what is line 12 all about, especially ws. I have never seen that in my programming class. Is it some variable?
Promt is a reference parameter, but you have it as cout..prompt.

I don't understand what you're asking here.


Furthermore, what is line 12 all about, especially ws. [...] Is it some variable?

Line 12 is all about what the comment says it is about. It extracts any leading whitespace from the input stream (std::cin, here.) This makes it impossible for the user to give us an empty string on line 13, so we don't have to check for that condition on line 15.

std::ws is what is called a stream manipulator.

http://www.cplusplus.com/reference/library/manipulators/
Last edited on
Topic archived. No new replies allowed.