Learning C++ Programming and I need some advice
The syntax is spectacularly simple as g++ SourceInFile -o BinaryOutFile so it's difficult to get that wrong.
You'd get exactly the same error if you typed g++ RabbitPieAndChips.cpp -o SomeSillyFilename.exe so it's not unreasonable to assume that the compiler can't read your source file, like it says.
As the source file has no path a compiler would look for it "here" - meaning, whichever directory you're sitting in as you type the command - so as others have suggested, check to see that it actually does exist where you expect it to.
You may find that Notepad has "intelligently" saved it to My Documents or somesuch and you're typing the command from another directory.
Something else to check - Notepad will sometimes inappropriately stick on .txt as the filetype so I wouldn't be surprised to discover you have a Motto.txt file instead of a Motto.cpp file. Or possibly even Motto.cpp.txt.
So from within the directory you think it's in, enter dir *.cpp and if you don't see it, try dir Motto*.* instead.
If neither shows any file starting with Motto then you've likely saved it somewhere else.
As someone else suggested, it's worthwhile creating a project directory named after the main source file (so: "Motto") or something else related to what you're doing and keep everything related to the project in there.
_________________
Giraffe: a ruminant with a view.
I just want to repeat what others have said, lol..
Problem 1. Notepad by default saves files with ".txt" at the end. When you are on the "Save"/"Save As" window you need to make sure the file name is "name.cpp" and the "save as type" is selected as "All Files" and not "Text Documents (.txt)" , like in this image:
http://www.linglom.com/images/Windows/T ... file/2.png
Notepad also sucks for anything except plain text files. Get yourself an advanced notepad program like Notepad++ or UltraEdit that supports something called "syntax highlighting" - where the code you type will be highlighted in a way that's easy on the eyes depending on what language it is.
It will also help you indent your code for you, so you know your first source that looks like this:
int main()
{
std::cout << "Solidum petit in profundis!\n";
return 0;
}
It will make it looks like this i.e. indented. Indenting makes it much easier to see the flow of the program.
int main()
{
std::cout << "Solidum petit in profundis!\n";
return 0;
}
Illustrating indenting.. Compare these two sources. Which is easier to follow?
int main()
{
for(int a=0;a<30;a++){
for(int b=0;b<30; b++){
if(b==10){
for(int d=0;d<30;d++){
}
}
for(c=0;c<30;c++){
}
}
}
}
int main()
{
for(int a=0;a<30;a++){
for(int b=0;b<30; b++){
if(b==10){
for(int d=0;d<30;d++){
}
}
for(c=0;c<30;c++){
}
}
}
}
Problem 2. Paths.
Your working directory (desktop) isn't the same place as where command prompt opens up. You saved your file to the Desktop (probably "C:\Documents and Settings\user\Desktop\") but the command prompt opens in "C:\Documents and Settings\user\". You could:
1) go into the Desktop folder by typing "cd Desktop," and compile from there, or you could
2) just compile from the "user" folder. That would require you to use the full paths, with quotes around them because command line applications like g++ don't like spaces. This would look like:
Better solution:
1. Download the Microsoft "Open Command Window Here" application. This will allow you to right click on the directory where you just saved your .cpp file, and open a command prompt with it's directory set as that directory, so you don't have to waste time "cd-ing" around the place. Check out this article: http://www.petri.co.il/add_command_prom ... plorer.htm
(petri is great btw)
2. Store your code in quick to get to directories.
"C:\Code\c++\" you'll find ends up being much better than "C:\Documents and Settings\user\Desktop\" or some other long path.
But, if you don't want to mess around with the command line then get yourself an IDE that includes a compiler, like Visual Studio or something free like Dev-CPP. Although I'd suggest you learn the hell out of command prompt and the CLI (command line interface) before doing this. This is just basically for you to learn Windows from a more technical side that will probably come in handy later.
Keep it up though!
If all the wonder suggestions other people have made fail or are impractical for some reason, try this: codepad.
Similar Topics | |
---|---|
Language learning apps and websites that don't use chatbots |
18 Dec 2024, 2:22 pm |
Should I take up my dad's advice on this? |
30 Jan 2025, 3:18 pm |
bad advice |
20 Feb 2025, 7:41 pm |
Advice with emotions |
06 Dec 2024, 9:04 am |