MY FIRST PROGRAM

Posts 1–5 of 5 · Page 1 of 1
MY FIRST PROGRAM
// first C++ program on Dev

#include <iostream.h>
using namespace std;

int main() // this is a main function, this is the function header
{
int carrots_per_pod, number_of_pods, total_carrots
cout << "Press Enter key after entering a number \n";
cout << "Enter Number of Pods: \n";
cin >> number_of_pods;
cout >> "Enter the number of carrots per pod: \n";
cin >> carrots_per_pod;

/* Declared all variables int types and allowed user input */

total_carrots = number_of_pods * carrots_per_pod

cout << "If you have \n";
cout << number_of_pods;
cout << "and " << endl;
cout << carrots_per_pod;
cout << " carrots in each pod, then" << endl;
cout << "you have ";
cout << total_carrots;
cout << " peas in all the pods." << endl;

return 1;
}
I think you meant to put
Code:
cout << "If you have \n";
cout << number_of_pods;
cout << " pods, and " << endl;


but looks good. You made this in Dev-C++ ?


-and the line cout >> "Enter the number of carrots per pod: \n"; // has the carrots (<<) backwards. (?)

-" total_carrots = number_of_pods * carrots_per_pod" is missing the terminating semi-colon.
-comments are usually placed before, or to the right of code, but doesn't really matter. Good use of comment(s) tho.
i used dev c++ and my compiler picked up the mistakes

the only problem i have is that the prgram closes immediately any suggestions?
yeah. the problem is, once your code is done, the program doesn't know it's automatically supposed to stay open. So you tell it to. There are 2 ways. The 1st / standard (?) way is to ask for user input (ie. use cin >> ), which will cause the form to stay open until the user gives input. or to, use a system call to "pause" and it'll say "Press any key to continue()" and then basically does the cin >> thing itsself.

Code:
...
cout << "you have ";
cout << total_carrots;
cout << " peas in all the pods." << endl;

return 1; // return = i'm done , 1 = "success" , 0 = "fail", for most people.
}
-----
Code:
...
cout << "you have ";
cout << total_carrots;
cout << " peas in all the pods." << endl;

system("PAUSE");
return 1;
}
or
Code:
...
cout << "you have ";
cout << total_carrots;
cout << " peas in all the pods." << endl;

cin >> [any variable]; // I'm not sure about cin.getline vs cin >> etc. Maybe investigate. 
return 1;
}
Posts 1–5 of 5 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us