# C++ Questions



## Crockeo (Apr 17, 2010)

Well, I have two questions about C++, they're quite specific.

1: How do I start another program via C++?

2: How do I store data in another file so on startup it has the data. (Context: I want to be able to tell my program where a file is located, and what the program should know it as)

Thank you in advance
-Crockeo


----------



## Ninjaboi (Dec 27, 2010)

I'll answer by number.

1. Not sure what your asking exactly. Are you asking on how to start a new project?

2. Your wanting to create a file and then use data that you store in it correct? Try going here for information in fstream, file-making, etc.


----------



## Crockeo (Apr 17, 2010)

I find this quite funny, this is like a parallel to when I was asking about VB2010. To clarify, what I'm wondering is how I start, lets say notepad (the example I used in my "VS2010 Question" thread) from code within a C++ project.


----------



## Ninjaboi (Dec 27, 2010)

Ah OK lol I didn't understand what you were asking. Here is a simple program to open up notepad:


```
#include <iostream>

int main()
{

    system("C:\\WINDOWS\\system32\\notepad.exe");

    return 0;

}
```
Tested in both VS2008 and Dev-C++ latest version.


----------



## Crockeo (Apr 17, 2010)

Thank you, I also had another question, although not relating to my original question (also parallel to my other topic). I've downloaded VS2010 C++ Express edition, and I really like it. Sadly I've noticed something that it doesn't do, which Dev C++ does.


```
#include <iostream>

int main()
{
     using namespace std;
     string X;
     cin >> X;
}
```
Something like that would give C++ Express 2010 an error, while Dev C++ would do fine with it. So my question is: Is there a way to store an input as a string in C++ Express 2010 without giving it an error?


----------



## Ninjaboi (Dec 27, 2010)

Yes there is a way. You just have to add:

#include <string>

at the top with your #include <iostream>.


----------



## Crockeo (Apr 17, 2010)

That's really quite easy, why do you think you need to include that for Visual C++ and not in Dev C++?

EDIT: Also, is there a way for me to clear the console? So if it said "Hello world" it would now say ""


----------



## Ninjaboi (Dec 27, 2010)

There is a way to clear the console. Try adding this line:

system("cls");

Not quite sure why it would allow you to use a string without including it. Though some environments pre-include several things to make things easier for the programmer.


----------



## Crockeo (Apr 17, 2010)

Thank you again, that's all I need for now.


----------



## Crockeo (Apr 17, 2010)

I have yet another question for the C++ guru if you care to answer it. The bold "loc" is saying that "Error: There are too many arguments in function call." which I understand, but don't know how to fix. My second problem is with the bold "string"s they tell me that "Error: Identifier 'string' is undeclared." which again I understand, but don't know how to fix. So my question is how I can fix those errors, and if unable, how would I do what I'm trying to do here, but with other code. (If you don't get what I'm trying to do from my code please, just ask me.


```
void add()
{
	using namespace std;
	void add2();
	string Loc;
	string Name;
	system("cls");
	cout << "Where is the file you wish to add?" << endl << "File Location: ";
	cin >> Loc;
	cout << endl << "File location added, what do you wish to call the file?" << endl << "File Name: ";
	cin >> Name;
	cout << endl << "Success! Your file has been added!";
	add2([B]Loc[/B],Name);
	Sleep(1250);
	main();
}

void add2([B]string[/B] X, [B]string[/B] Y)
{

}
```
Thank you, whoever answers this (my bet is on Ninjaboi)


----------



## Ninjaboi (Dec 27, 2010)

"Error: Identifier 'string' is undeclared."

In the pre-processor, try adding '#include <string>;'. If you've already done that, try putting either 'std::' behind string ( std::string ) or just put 'using namespace std' in your program as a global so that you can just call string by itself ( string ).

"Error: There are too many arguments in function call."

Try fixing the first problem I stated, and see if this problem goes away. If not, say so.


----------



## Crockeo (Apr 17, 2010)

I tried what you said, but it still doesn't work. Any other suggestions?


----------



## Ninjaboi (Dec 27, 2010)

Here is a sample program I made to help you out. It shows an example of using the std namespace as a global for you program, as well as declaring a method so it can be used.


```
#include <iostream>
#include <string>

using namespace std;

string input();

int main()
{

    cout << "What is your name?" << endl;

    string name = input();

    cin.get();

    cout << "Hello, " << name << "!" << endl;

    cin.get();

    return 0;

}

string input()
{

    string input;

    cin >> input;

    return input;

}
```
Are you sure you put 'using namespace std' as a global? Did you also declare the method so that it can be used in the code that's before it?


----------



## Crockeo (Apr 17, 2010)

Thank you for that, but I decided it would be easier to just do it all in one sub (although a little messier) but I just tried to implement some of the fstream stuff you taught me about before, and I can't seem to get it to work. If you wouldn't mind, could you try to help me figure out why it's not working.

```
ofstream launchlist("LaunchList", ios::out);
	launchlist << X << "|" << Y;
```
I checked that X and Y got their values correctly, and everything else is working fine. It's just that after the file LaunchList gets made, nothing is written to it.


----------



## Ninjaboi (Dec 27, 2010)

Did you examine the practice/sample project on the page I linked you to?


```
#include <fstream>

#include <iostream>

using namespace std;



int main()

{

    char FirstName[30], LastName[30];

    int Age;

    char FileName[20];



    cout << "Enter First Name: ";

    cin >> FirstName;

    cout << "Enter Last Name:  ";

    cin >> LastName;

    cout << "Enter Age:        ";

    cin >> Age;



    cout << "\nEnter the name of the file you want to create: ";

    cin >> FileName;

    ofstream Students(FileName, ios::out);

    Students << FirstName << "\n" << LastName << "\n" << Age;



    cout << "\n\n";

    return 0;

}
```
It provides a nice sample of how it should be set up. From the code snippet your provided, I'd say it should work fine.


----------



## Crockeo (Apr 17, 2010)

I based it off of the example, but sadly it does not work. Also, I was thinking, it was something with the adding line, as it makes the file, but doesn't write to it, so if you wouldn't mind, could you make some code that you know yourself works to write to a file?


----------



## Ninjaboi (Dec 27, 2010)

Sure thing. This should be similar to what your trying to do ( both direct input using "" and variables such as 'int a' in this program ):


```
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{

    int a = 1;

    ofstream hello("hello.txt", ios::out);

    hello << "Hello World! " << a;

    return 0;

}
```
Tell me if that works for you, and if it's showing you what your wanting to see.


----------



## Crockeo (Apr 17, 2010)

It worked when I used your exact code. But when I changed it like 

```
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{

    int a = 1;

    ofstream hello("hello.txt", ios::out);

    hello << "Hello World! " << a;

    [B]char response;

    cin >> response;[/B]

    return 0;

}
```
What i changed is in bold, and when I did that it stopped working. I generally put that in to pause a program. So could it be a connection between something else in the function that's messing it up? Here's the function:

```
void add()
{
	using namespace std;
	system("cls");
	string X;
	string Y;
	cout << "Please enter the location of the file you wish to add." << endl << "Location: ";
	cin >> X;
		cout << "Please enter the name you wish to refer to this file as." << endl << "File: ";
		cin >> Y;
		cout << endl << "File name accepted." << endl << "Adding file name and location to the file list.";
		//Adding code stuffs
		ofstream launchlist("LaunchList.txt", ios::app);
		launchlist << X << "|" << Y;
		//Done adding code stuffs
		Sleep(2000);
		main();
}
```


----------



## Ninjaboi (Dec 27, 2010)

Try replacing the part you put in bold in your first code you snipped with:

cin.get();

That's what I use when I want the user to put input in that's not needed.


----------



## Crockeo (Apr 17, 2010)

Thank you for that suggestion, although I don't need to use it now, I will later. But what I'm asking is what could it be in my function that messes up the writing to the file?

And also, thank you for all the help Ninjaboi.


----------



## Ninjaboi (Dec 27, 2010)

It wrote to the file specified just fine for me. Honestly, there are several small problems both with organization and with the overall attempt at the project itself, but for your question "Why won't it write to the file?" it works just fine:


```
#include <iostream>
#include <string>
#include <fstream>
#include "windows.h" 

using namespace std;

void add();

int main()
{

    add();

    return 0;

}

void add()
{
    using namespace std;
    system("cls");
    string X;
    string Y;
    cout << "Please enter the location of the file you wish to add." << endl << "Location: ";
    cin >> X;
        cout << "Please enter the name you wish to refer to this file as." << endl << "File: ";
        cin >> Y;
        cout << endl << "File name accepted." << endl << "Adding file name and location to the file list.";
        //Adding code stuffs
        ofstream launchlist("LaunchList.txt", ios::app);
        launchlist << X << "|" << Y;
        //Done adding code stuffs
        Sleep(2000);
        main();
}
```
That's what I did with it, and it does write to the file just fine. I personally would make changes to it, but only if your wanting me to do so.


----------



## Crockeo (Apr 17, 2010)

Besides the adding of an int main() to call the add() function, I don't really see what else is edited, so if you wouldn't mind, could you either tell me what you edited or you could edit my whole source. Also do you have any ideas for organization for me?

The source code is at: Launcher.cpp - File Shared from Box.net - Free Online File Storage (it's just a .cpp file)


----------



## Ninjaboi (Dec 27, 2010)

I didn't change anything, just made it where I could compile it. I was wanting to see if you were interested in me changing/fixing it up a bit.

For organization, it's all based on what looks right for you. The source you provided looks good with what you've got so far. I would of done some other things to make it easier to read, but for now that's good.

The only thing I see wrong with your code is line 59 'string X;' It won't be able to convert to const char *. You'll have to do something like 'char X[200];' See if that works for you. If that wasn't an issue for you, then don't worry about it. I only mentioned it because it's in add(), which was the function your having trouble with.

Aside from that, it should work. I compiled it just fine. It created a launch list for me, with the location and name of the file I wanted. Nice job by the way!


----------



## Crockeo (Apr 17, 2010)

Although I changed something (I have no idea what) and it worked, still, thank you for all the help. I don't know where I'd be without C++ geniuses like you.

Oh, and can you give me help on my remove section? Like, how would I take away a line from the file?


----------



## Ninjaboi (Dec 27, 2010)

delete a line from a text file - C++

That's a good example of how to do just that. His code is more or less good for learning, but I think it should be good enough.


----------



## Crockeo (Apr 17, 2010)

Thank you again!


----------



## Crockeo (Apr 17, 2010)

And finally, my last questions, so this (learning) project can be complete, how do I launch something via a variable (system([variable]) doesn't work).

And:

```
C:\User\[Myusername]\Desktop\Minecraft.exe|Minecraft
```
How would I get the location from that line, if I typed in "Minecraft" (what's on the left) how would I make it so my program understands that I would want the location, separated via the bracket.


----------



## Crockeo (Apr 17, 2010)

Sorry for the double post, but I solved both of those problems. But a new problem arose after I changed the way I print and get from the launchlist.txt file.

I made it so the location and name are on different lines, so I can use the getline command, but I can't seem to get it working, might you be able to give me an example where it gets a line from a program and assigns it to a string?


----------



## Ninjaboi (Dec 27, 2010)

I'm a bit fuzzy of what your asking. Are you wanting to get data from say a .txt file and set a variable as that data? If so I can help give examples of just that.


----------



## Crockeo (Apr 17, 2010)

Well, I figured out how to do all the the getline stuff, but I found out I didn't need it. What I do need now is to know why my if/then statement isn't working, here's my code:

```
void launch()
{
	using namespace std;
	system("cls");
	char X[2049];
	char Name[2049];
	char Loc[2049];
	cout << "What file do you want to launch?" << endl << "Name/Location: ";
	cin >> X;
	ifstream Exists(X);
	if (Exists)
	{
		system(X);
		Sleep(1500);
		exit(1);
	}
	else
	{
		while (1 == 1)
		{
			ifstream LL;
			LL.open("launchlist.txt");
			LL >> Loc;
			LL >> Name;
			cout << Name << X;
			if (Name == X)
			{
				system(Loc);
				Sleep(1500);
				exit(1);
			}
		}
		main();
	}
}
```
I've tested it, and even when Name equals X it doesn't perform the if/then. Any ideas? Oh, and sorry for my lack of specifics, that happens while you're in a frat house huh?


----------



## Ninjaboi (Dec 27, 2010)

I'd take a look at:



> ifstream Exists(X);
> if (Exists)


Do you get a compile error? Is this just a logic problem?

Don't worry about the lack of specifics, I'll ask for anything if I need the information. Of course, any details given prior might make it easier on myself lol. I'm glad your asking lots of questions, it gives me something to look forward to .


----------



## Crockeo (Apr 17, 2010)

I recently found out via testing that comparing a char to another char doesn't work within if/then statements, so I changed it around so the things I was comparing are both strings, but they get converted into chars for the location (because strings can't be used in things like "system" and "ShellExecute").

Two things, first off, back to a former question, as I realized that in 'Program Files' there's a space, and I'm just getting the first word of that, how do I use the 'getline' function to get something from a .txt file?

Second, when/how do you think I should get into Windows Forms and such?


----------



## Ninjaboi (Dec 27, 2010)

C++ text file line by line/each line to string/array

That link shows a few examples. You could also try going here. If you still don't get it, tell me and I'll try to explain it myself.

For now you should focus on the language itself, learning it fully or at least close to it. Afterwords, you should decide what plaform you wish to start programming on. Since you mentioned Windows, I'd like to tell you that programming with the WinAPI is very messy. I personally hate how messy it is. You also have the big overhead of OS specific variables you'd have to learn, which is also tedious. I'd highly suggest waiting for a while before going into an OS specific program. After a while it won't feel as bad doing it, but ask anyone when they say it's such a mess even after you get the hang of it. There are libraries that provide an overhead over these things ( basically a layer over the messy code that you can call with your code and make it neater/cleaner ) which do help.

So to answer your question, I'd say until you can use C++ well, and can do console projects easily. Trust me when I say it's better to wait. I tried to delve right into it my first go, and didn't even understand the language itself, and failed at it lol.


----------



## Crockeo (Apr 17, 2010)

Thank you for both your help on 'getline' and whether or not to go to Java.

I tried Java a little bit. It was pretty easy to learn considering that I have prior experience with the very similar language of C++, but I think I like C++ more still. So I think I'm gonna stay with that.


----------



## Crockeo (Apr 17, 2010)

I just realized that I have another question, well two.

The first is: does 'cin' take the whole line, or just before a space.

The second is: if not, how would I make it take a whole line in rather than the first part before a space?


----------



## Ninjaboi (Dec 27, 2010)

'cin' will take spaces. If your working with a string, you can type in:

"Ninja Boy"

It will work just fine.


----------



## Crockeo (Apr 17, 2010)

Alright, so that part of my program will work fine, but why is it that when I enter a file location with spaces it spazzes out?


----------



## Crockeo (Apr 17, 2010)

I just realized, you'll probably need code:

```
void add()
{
	using namespace std;
	system("cls");
	string X;
	string Y;
	string Z;
	cout << "Please enter the location of the file you wish to add." << endl << "Location: ";
	cin >> X;
	ifstream Exists(X);
	if (Exists)
	{
		cout << "Please enter the name you wish to refer to this file as." << endl << "Name: ";
		cin >> Y;
		cout << endl << "File name accepted." << endl << "Adding file name and location to the file list.";
		ofstream LL("launchlist.txt", ios::app);
		LL << X << "\n" << Y << endl;
		Sleep(2000);
		main();
	}
	else
	{
		system("cls");
		cout << "ERROR: File not found!" << endl << endl;
		while (1 == 1)
		{
			cout << "Do you want to try again or go back to the menu?" << endl << "'Again' or 'Menu': ";
			cin >> Z;
			if (Z == "Again")
			{
				add();
			}
			else if (Z == "Menu")
			{
				main();
			}
			else
			{
				system("cls");
				cout << "ERROR: Command not recognized!";
				Sleep(1250);
				system("cls");
			}
		}
	}
}
```
The whole code of the function that's messing up.


----------



## Crockeo (Apr 17, 2010)

Sorry for the triple post, but I used Java a bit more, and I really liked it, I'm learning much quicker, mostly because of the stuff you're teaching me, so thank you. I'll still be working with C++ some, but I have a feeling I'll be mainly Java, oh and one final question to top it off: Where's a good place to learn Java?


----------



## Ninjaboi (Dec 27, 2010)

Go to the Getting started thread, and go down to Resources for some good starting points. I've been considering re-working that thread so that it will include much more information, a few more languages, and many more references ( as well as a different structure ).

I personally recommend Java for learning purposes, and only after getting programming concepts down using would I tell the user to consider another language if they don't like it.

For your location spacing issue, try putting a '_' underscore instead of a space, and see what that does for you. If that doesn't work, tell me and I'll try out that function you provided myself.


----------



## Crockeo (Apr 17, 2010)

Oh, and do you know if anyone's coded something as a plugin for VS2010 to be able to do java stuff?


----------



## Crockeo (Apr 17, 2010)

(back to C++) Also, I just tested whether it would work or not with underscores instead of spaces and it still did not, mind trying to get it to work on your end then giving me the code that you changed? Or if you would rather, trying to explain it to me.


----------



## Ninjaboi (Dec 27, 2010)

Crockeo said:


> (back to C++) Also, I just tested whether it would work or not with underscores instead of spaces and it still did not, mind trying to get it to work on your end then giving me the code that you changed? Or if you would rather, trying to explain it to me.


Sure thing, I'll be doing it later on today though, as I'm kind of booked at the moment.

As for the VS2010 Java programming question, try looking at this.


----------



## Crockeo (Apr 17, 2010)

Thank you again all mighty C++ guru.


----------



## Ninjaboi (Dec 27, 2010)

From what I'm thinking, it's a fairly complicated issue.

When I type in "Hello" it simply cannot find the file ( it doesn't exist ). However, when I type "Hell o" it will go to "ERROR: Command Not Recognized". My best guess is it takes "Hell" as the first entry ( X ) and "o" as the second entry ( Z ), as it also goes straight through the request for Z. Thus making the one entry seem like two, and therefore going through the original line:


```
cin >> X;
```
which would take "Hell o" just fine.

After, you'd go to this line:


```
ifstream Exists(X);
```
which from my guess will only take everything before the space. So that would leave it with being "Hell". This leaves "o" as the leftover entry. Going to line:


```
cin >> Z;
```
it would take "o" and use it, as it's what you've entered ( even though you haven't truly typed anything else in ) and assigns Z "o". Since that is not a command, you'll get the error:



> ERROR: Command Not Recognized


As you've programming it to do.

Does that make sense? I'll probably make a small program that tests this theory ( though I remember running across this when I read about strings in both Java and C++ ). I'll also make a program that fixes it and allows you to use spaces for your input and use it in ifstream ( which I thought would work in the first place ).

Sorry I had no definite fix at the moment. I'll be working at it, because now it's getting me curious as to what the issue is.

EDIT: I remember something about flushing the stream for entering in strings, and it might also be required for all streams. I'll have to look that up again .


----------



## Crockeo (Apr 17, 2010)

Thanks for your answer, your reasoning makes sense. I'll be trying to fix this problem along side you, it'll be a race, that you'll probably win.

EDIT: I made a program whether your theory is correct, and it is. Now we must find the solution.


----------



## Ninjaboi (Dec 27, 2010)

Lol then let it begin!

Also, I'd like to point out that it may lead to you ( or I ) to make a method that will go through the string you type in and replace all spaces with underscores ( or remove them ) that way it won't be an issue. However, that means that ( when it comes to files ) you'll have to make them without spaces, but I'm sure there is a way around that.

Best of luck to you!


----------



## Crockeo (Apr 17, 2010)

I have a feeling that our solution won't come from making spaces underscores, but it will come in the form of us working out why getline doesn't work in my program. Although we should explore all possible solutions.

I've made other programs with getline, and it works wonders.

Oh, and an off-topic question, what's the best C++ compiler for Linux? (Specifically Ubuntu) I officially hate Vista in every way shape in form, and I want to try Linux, but my main concern is whether there's a good C++ compiler for it.


----------



## Crockeo (Apr 17, 2010)

Such a simple answer!!! It was just because the variable on the main() was the same as on the add() function. I don't know if it completely works yet, but I know that getline works now.

EDIT: I tested it and it works perfectly fine now. But I've run into another problem, now that we can add things with spaces in their name, we have to be able to call things with spaces in their name in the launch() function.


----------



## Ninjaboi (Dec 27, 2010)

Crockeo said:


> I have a feeling that our solution won't come from making spaces underscores, but it will come in the form of us working out why getline doesn't work in my program. Although we should explore all possible solutions.
> 
> I've made other programs with getline, and it works wonders.
> 
> Oh, and an off-topic question, what's the best C++ compiler for Linux? (Specifically Ubuntu) I officially hate Vista in every way shape in form, and I want to try Linux, but my main concern is whether there's a good C++ compiler for it.


GCC is a good one.

http://www.augustcouncil.com/~tgibson/tutorial/iotips.html

That links gives a great explaining of how streams work.

So I was right by saying we would need to 'flush' the stream. Try that and see how it works for you ( it worked for me  ).

EDIT: Great! Glad that parts working. I found several ways to fix the problem in whole, but they were either tedious or required another method ( which I didn't want you to have to make another one ). Anyways, is it all cleared up now?


----------



## Crockeo (Apr 17, 2010)

Well, as I said, the add() function is working, but the the launch() function kinda fails now. I'm working to fix it right now. And I'll be installing Ubuntu very soon.


----------



## Ninjaboi (Dec 27, 2010)

Sounds like a plan to me. I personally prefer the Linux OS compared to Windows. The only two exceptions is that games are mainly made for Windows and that many USB devices don't have drivers for Linux.


----------



## Crockeo (Apr 17, 2010)

Ubuntu failed, but I am in a Linux right now, and I REALLY like it, I may try to make multiple partitions so I can go to Windows when I don't want to play Minecraft or code C++.

Back on topic:
Do you know if something like

```
getline(LL,X);
```
 would work?


----------



## Ninjaboi (Dec 27, 2010)

Why are you using a 64 bit integral type as the first parameter?

It might work, tell me if it does.


----------



## Crockeo (Apr 17, 2010)

Huh what?


----------



## Ninjaboi (Dec 27, 2010)

C++ Integer Constants


----------



## Crockeo (Apr 17, 2010)

Ah, I see what you mean, in my program I use LL to refer to the "launchlist.txt" file. Should I change that?


----------



## Ninjaboi (Dec 27, 2010)

Lol well it was a bit confusing when I looked at it, but now I understand now that you've told me what your using it for. I'd suggest changing it if possible, but it won't harm anyone if you just comment decently throughout your programs.


----------

