My first program... I compiled it.. there are no errors... but when I click Run, nothing happens at all... I am using Dev C++


Code:

#include <iostream>

using std::cout;
using std::endl;

int main()

    int cats, dogs;
    int animals;
   
    cats = 5, dogs = 6;
    animals = cats + dogs;
   
    cout << endl;
    cout << "Josh has " << animals << " animals in all.";
    cout << endl;
   
    return 0;
}


Edit by Tanner: changed topic title
Windows, I assume? Did you open it via a command prompt? Or just click "Run" from the context menu or double click?
I tried clicking the "execute" drop down menu, then click run... I tried the hot key(s) which are Ctrl+f10 and still nothing. Just nothing happens. I even threw in a

Code:

system("pause")

after the last cout.. How would I run it via CMD?

EDIT: yes, I run windows XP, sorry Smile

Code:
#include <iostream>

using std::cout;
using std::endl;

int main()
{   
    int cats, dogs;
    int animals;
     
    cats = 5, dogs = 6;
    animals = cats + dogs;
     
    cout << endl;
    cout << "Josh has " << animals << " animals in all.";
    cout << endl;
     
    return 0;
}


This program has a very common error. You need to declare a namespace.

I really won't talk about the details of namespaces, but this works:


Code:
#include <iostream>

using namespace std;    //This is very important

int main()
{
    int cats = 5;
    int dogs = 6;
    int animals = cats+dogs;

    cout << "Josh has " << animals << " animals in all.";
    return 0;
}


You also don't need to say you will use cout and endl since they are both included in iostream.

I also shortened the code a little bit and I think you can understand it Smile

This would be ok too:


Code:
#include <iostream>

using namespace std;    //This is very important

int main()
{
    int cats = 5;
    int dogs = 6;

    cout << "Josh has " << cats+dogs << " animals in all.";
    return 0;
}


Hope you got it Smile

EDIT:

Some information on namespace:

Quote:
All the elements of the standard C++ library are declared within what is called a namespace, the namespace with the name std. So in order to access its functionality we declare with this expression that we will be using these entities. This line is very frequent in C++ programs that use the standard library, and in fact it will be included in most of the source codes included in these tutorials.


I found it here.
To be honest ScoutDavid both of those are correct. You don't actually need to have
Code:
using namespace std;
line. If you wanted you could possibly go through and type
Code:
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::getline;
using std::fixed;
using std::setprecision;
using std::transform;


And then be able to use those perfectly fine. The only difference is the way you did it is shorter and much easier than remembering to type all that. And the issue joshie is having is more than likely it is running, but seeing as it runs so quick, the command prompt is opening and then closing extremely fast.

And to run it via command prompt joshie all you would need to do is start>run>cmd. Then use cd to navigate to its directory and then just type its name.
I tried his original code and there were lots of errors, due to the lack of namespace.

Either way, to avoid closing so fast, you can also add an input line at the end:


Code:

getchar();
Also, I assumed the usage of animals was to work with more variables and such, though that may just be me. I don't really see the point of shortening that part any more because it is already a single-use program, why make it even shorter? But oh well, Joshie, did what everyone else said help?
_player1537 wrote:
Also, I assumed the usage of animals was to work with more variables and such, though that may just be me. I don't really see the point of shortening that part any more because it is already a single-use program, why make it even shorter? But oh well, Joshie, did what everyone else said help?


Yeah I agree, I was just adding information, you can give variables values when declaring them.
Thanks for all the input guys, it's nice to know I have you're guys knowledge for backup. Here is my revised program.


Code:

#include <iostream>

using namespace std;    //This is very important

int main()
{
    int cats = 5;
    int dogs = 6;
    int animals = cats+dogs;

    cout << "Josh has " << animals << " animals in all.";
    system("pause");
    getchar();
}


It says there is an error on line 2, which is blank... but the error description is ...
FILE: C:\Documents and settings\Joshua.......... Unable to run program.
this stuff is confusing Sad
I reallly think you should use another IDE/Compiler. Use Code:Blocks over GNU or use notepad together with GNU Compiler.
Think you could post me a link, Scout? I really want to make sure I'm downloading the correct file. Thanks Smile
Direct link for download YAY

You'll want this.
Much appreciation Scout, thanks again.
Also, avoid using sysem("pause");, use:

cin.get()

OR

getchar()
I'm guessing both of those will wait for the user to push a key?
joshie75 wrote:
I'm guessing both of those will wait for the user to push a key?


Yap Smile
Very Happy You're awesome. I'm going to get to work on some starter programs.
joshie75 wrote:
Very Happy You're awesome. I'm going to get to work on some starter programs.


Did Code::Blocks work? And no I'm not awesome Smile
Yes code blocks works amazingly; I looked for a GNU compiler download but couldn't find one, so ATM I am downloading Windows SP3 for VS 1010 compiler. Smile
The Code::Blocks version I sent you had all included Smile

Also, based on your earlier program I made one that works with input:


Code:
#include <iostream>

using namespace std;

int main() {
  int dogs;                //This variable holds number of dogs
  int cats;                //This variable holds number of cats

  cout << "Dogs: ";
  cin >> dogs;

  cout << "Cats: ";
  cin >> cats;

  if (dogs>=0 && cats>=0)
  {
    cout << "Your total number of animals is: " << dogs+cats << endl;
  }
  else
  {
    cout << "Invalid number of animals" << endl;
  }

  return 0;
}


What do you think? Add a cin.get() in the end so that it doesn't quit.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 3
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement