What's new

I want to learn some programming

OP
xneoangel

xneoangel

Persona User
Ok i'm kinda stuck with this code, can you guys help me a bit?


#include <iostream>
using namespace std;

int main ()
{
int n;
cout << "Enter the starting number > "; //i don't understand what the > "; part does
cin >> n;

while (n>0) {
cout << n << ", "; /*and this the other line i don't understand, i know what cout << n does but the << ", " part confuses me*/
--n;
}

cout << "FIRE!";
return 0;
}

Thanks Toasty the system("pause") command works like a charm.
 

Doomulation

?????????????????????????
cout outputs the text (or variables) that you specify to the screen. Double quotes (looks like this: ") indicates a string. You got that? Okay.

Code:
cout << "Enter the starting number > "; // Prints the string "Enter the starting number > " to the screen.
cout << n << ", "; // First print the value of variable n, then output the string ", " to the screen.

Again, I urge you to consider using string buffers for input via cin unless you want to mess with clearing buffers.
 
OP
xneoangel

xneoangel

Persona User
>_<, ok i'm a little embarassed, that was one stupid mistake.

Doomulation said:
Again, I urge you to consider using string buffers for input via cin unless you want to mess with clearing buffers.

By that you mean that i should store stuff in strings via cin instead of in variables?
 

Doomulation

?????????????????????????
xneoangel said:
By that you mean that i should store stuff in strings via cin instead of in variables?
That is correct. Have you tried inputting a string when using cin >> buf (which is an integer)? It will mess up big time. Therefore, I recommend using a buffer of chars, then you can later use atoi to convert to a number if you are expecting it.

And if the user did not enter a number, you can check for...

Code:
if (atoi(buf) == 0 && buf[0] != '0') // Error occoured. Perhaps the entered data is not a number?
 
OP
xneoangel

xneoangel

Persona User
Ok thanks.

Now i have another question, i'm doing the tutorial at www.cplusplus.com, but a friend of mine borrowed me these books, Sams-Teach Yourself C++ in 21 Days 2nd Edition, Sams-C++ Primer Plus Fourth Edition and McGrawHill-C++ from the ground up.

Now my question is, should i continue with the tutorial or stop it and start with one of these books?
 

Poobah

New member
The 21 days one is the very same one I recommended and posted earlier, so I'd recommend that one. Since you have convenient access to both, you should see which ones you find the best and use those.
 
OP
xneoangel

xneoangel

Persona User
Yeah well C++ Primer plus and C++ from the ground up, seem like great books, the thing is that they are 800 pages long :plain:

And the 21 days one seems good to but it is outdated, it uses iostream.h instead of iostream and it doesn't use the namespace std, and well i dont know if there is anything else.
 
Last edited:

Doomulation

?????????????????????????
I don't think it matters. Your goal is to learn the language, which is probably what it will do.
When it comes to Windows programming you'll enter a whole new world and there is no book in the entire world that can explain everything.
 

Poobah

New member
But it isn't too hard to pick up if you get the documentation from the platform SDK's downloader. I found that theForger's Win32 API tutorial was very helpful for kick-starting me into Windows programming. A resource editor like ResEdit also makes things a lot easier.
 
Last edited:

Doomulation

?????????????????????????
Win32 Programming is much harder than a bunch of simple tutorials. It requires you know such things as what deadlocks are, what message pumping is, how to handle the GUI and messagages, etc, and why other programs FREEZE when one program FREEZES and they've done nothing wrong. Ah yes, I forgot, that's Windows fault.
 
OP
xneoangel

xneoangel

Persona User
Ok i started wiht one book but well i'm stuck at this exercise:

Exercise said:
Write a program to ask the user to enter a series of numbers. Print a message saying how many of the numbers are negative numbers.

It's about the if statement, but i don't know how to do that.
can you guys help me a bit here?

I can get the program to identify which numbers are negative and which are possitive... but to count how many are negative well that confuses me.
 
Last edited:

Toasty

Sony battery
Start out with something like this:
Code:
int numberOfNegatives = 0;
Then, every time you read a number from the user do something like this:
Code:
if(numberFromUser < 0) ++numberOfNegatives;
/* ++numberOfNegatives basically means numberOfNegatives = numberOfNegatives + 1 */
Then after you have read all of your numbers, print the value of numberOfNegatives.
 

Poobah

New member
Doomulation said:
Win32 Programming is much harder than a bunch of simple tutorials. It requires you know such things as what deadlocks are, what message pumping is, how to handle the GUI and messagages, etc, and why other programs FREEZE when one program FREEZES and they've done nothing wrong. Ah yes, I forgot, that's Windows fault.
I found that it wasn't harder than a bunch of tutorials and the official documentation, but each to his own.

What's a deadlock in terms of the Win32 API?
 
OP
xneoangel

xneoangel

Persona User
Toasty said:
Start out with something like this:
Code:
int numberOfNegatives = 0;
Then, every time you read a number from the user do something like this:
Code:
if(numberFromUser < 0) ++numberOfNegatives;
/* ++numberOfNegatives basically means numberOfNegatives = numberOfNegatives + 1 */
Then after you have read all of your numbers, print the value of numberOfNegatives.

Ok if i do something like this:
while (cin >> numberfromuser) // to have an undefined number of inputs in the variable
and then put the code you told me, will it add 1 to numberofnegatives per each negative number?

Because it isn't working, or maybe i did something wrong:

MyCode said:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{int a, num = 0;
cout << "Write a series of numbers separated by spaces:" << endl;
while (cin >> a)
if (a<0)
{++num;
cout << num;}
else
{cout << "You didn't input any negative number" << endl;}
system("PAUSE");
return 0;}
 
Last edited:

Poobah

New member
What's wrong with that code? It works fine for me. (You might want to use [php] tags so that the code you post is easier to read.)
 
OP
xneoangel

xneoangel

Persona User
Well yes it compiles, but it doesn't do what i thought it would do.
I need it count how many negative numbers i enter and then display that number but if let's say i write two negative numbers, the number it displays it's 12 and not 2.
 

Poobah

New member
As I said earlier, it's working fine for me. Perhaps what is confusing you is that you forgot to output a new line character after displaying the number of negative numbers.

EDIT: Oh, I see. I was entering one number per line. You could change it to output the count after all the numbers have been entered, but adding the new line character should also be sufficient.
 
Last edited:
OP
xneoangel

xneoangel

Persona User
Hmm yes it kinda works but if i input 3 negative numbers i get 123 and if input 2 i get 12 is there a way to make it only display the last number? which is the number of negative digits, also if i input any positive number i get the You didn't input any negative number established in the if condition but i thought it'd only display it if i didn't input any negative numbers but it appears if there is any positive number, how can i fix that?
 
Last edited:

Toasty

Sony battery
The output doesn't look very pretty, but it does seem to produce a correct end result for me as well. For the sequence:
12 -4 6 7 -8

I got the output:
You didn't input any negative number
1You didn't input any negative number
You didn't input any negative number
2


It could use some refinement (I'd get rid of the "You didn't input any negative number" line and print endl after you print num), but it does the job.
 
OP
xneoangel

xneoangel

Persona User
Ok here is an updated version of the code
MyCode said:
using std::cout;
using std::cin;
using std::endl;
int main()
{int a, num = 0;
cout << "Write a series of numbers separated by spaces:" << endl;
while (cin >> a)
if (a<0)
{++num;
cout << "You inputted " << num << " negative numbers" << endl;}
system("PAUSE");
return 0;}

If i input two negative numbers i get this result:
You inputted 1 negative numbers
You inputted 2 negative numbers

But i only want to get the second sentence which is the correct number of negative numbers i inputed.
 

Top