Learning C++ Programming and I need some advice

Page 2 of 2 [ 20 posts ]  Go to page Previous  1, 2

Cornflake
Administrator
Administrator

User avatar

Joined: 30 Oct 2010
Gender: Male
Posts: 65,711
Location: Over there

17 May 2012, 4:11 pm

The syntax is spectacularly simple as g++ SourceInFile -o BinaryOutFile so it's difficult to get that wrong. :wink:

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.


cooldryplace
Blue Jay
Blue Jay

User avatar

Joined: 18 Mar 2011
Age: 35
Gender: Male
Posts: 84

20 May 2012, 1:52 am

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:

Code:
#include <iostream>

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.
Code:
#include <iostream>

int main()
{
   std::cout << "Solidum petit in profundis!\n";
   return 0;
}




Illustrating indenting.. Compare these two sources. Which is easier to follow?
Code:
#include <iostream>

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++){   

}
}
}
}


Code:
#include <iostream>

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:

Quote:
g++ "C:\Documents and Settings\user\Desktop\Motto.cpp" -o "C:\Documents and Settings\user\Desktop\Motto.exe"


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!



techn0teen
Veteran
Veteran

User avatar

Joined: 14 Sep 2010
Age: 32
Gender: Male
Posts: 663

20 May 2012, 11:54 pm

If all the wonder suggestions other people have made fail or are impractical for some reason, try this: codepad.



Cornflake
Administrator
Administrator

User avatar

Joined: 30 Oct 2010
Gender: Male
Posts: 65,711
Location: Over there

20 May 2012, 11:57 pm

I think you may have the wrong link there: currently it points to this:

Image
Neat picture, but not much of an editor... :wink:


_________________
Giraffe: a ruminant with a view.