PDA

View Full Version : Looping program.. ONOZ!



Vid Gamer
01-03-04, 01:10
I'm seriously about to ditch this class because everything seems easy in class and then when I go to actually write the program nothing feckin' works! :mad:


I have to write a program, using a for loop, which will ask the user to enter 25 integers (I'm using 5 to test it, atm). The program will then print out the smallest and largest integer entered, the product of the integers and the number of times the user enters the number 10.


Sounds easy to be honest, but for one the actual program won't even loop no matter what I try! Here's what I have so far:


//Christopher Singleton
//For Loop Assignment

#include<iostream.h>
#include<iomanip.h>
int main ()
{
int number;
int largeNumber;
int smallNumber;
int product;

cout.setf(ios::right|ios::showpoint|ios::fixed);
cout.precision(2);


for(i=0;i<5;i++)
{
cout<<"Enter a whole number. ";
cin>>number;
product=number*1;
largeNumber=number+1;
smallNumber=number-1;
cout<<"Your smallest integer is "<<smallNumber<<endl;
cout<<"Your largest integer is "<<largeNumber<<endl;
cout<<"The product of the intergers are "<<product<<endl;
}
return 0;
}

Apart from the fact it won't loop, I have a few other questions:

1. Am I doing the smallest/largest integer part right? It's the only way I see how to do it.
2. How in God's name do I print out how many times the user enters 10? How do I even start that?

Thanks for anyone that helps. :)

joran420
01-03-04, 01:29
no





dont think you need this code:
cout.setf(ios::right|ios::showpoint|ios::fixed);
cout.precision(2);

fix this:
in>>number;
if (i == 0)
smallestnumber = number;
else if (number < smallestnumber)
smallestnumber = number;


if (i == 0)
largestnumber = number;
else if (number > largestnumber)
largestnumber = number;



product*=number;


and this is why it wont loop :
for(int i=0;i<5;i++)


that'll be 10.00 for doin yer homework

Kalamazoo
01-03-04, 01:31
declare i

Vid Gamer
01-03-04, 01:32
Thanks but do u mind editing that and putting it in the [code] format so it's easier to read?

Kalamazoo
01-03-04, 01:35
ROFL

G.0.D.
01-03-04, 01:35
What level of class is this?
Uni or high school.

MortuusLupus
01-03-04, 02:10
Also, your for loop's only gonna be looping 5 times, not 25. And to count the number of times 10's entered, just do

int count; //declared outside the loop

for(.....)
{
....
if( number == 10)
count++;


.....
}

Shujin
01-03-04, 02:20
[ edited for violation of the forum rules (spam) - use PMs please ]

Vid Gamer
01-03-04, 02:30
joran,

outputting the large number works but it doesn't for the smallest number. using product*=number doesn't work either.

and Mortuus,

that code is confusing, I tried putting it in but doesn't work, I probably put it in wrong though.

MortuusLupus
01-03-04, 02:50
Please note that you really oughtn't be coming here for things like this. If you're having difficulty with the assignments, go see your TA or instructer during their office hours (if you're in college), or your teacher if you're still in high school.




#include<iostream.h>
int main ()
{
int number;
int largeNumber;
int smallNumber;
int product;
int count = 0;

for(int i=0; i<25; i++)
{
cout<<"Enter a whole number. ";
cin>>number;


if(i = 0)
{
smallNumber = Number;
largeNumber = Number;
}

if(number > largeNumber)
largeNumber = number;

if(number < smallNumber)
smallNumber = number;

if(number == 10)
count++;


product = number * procuct;
}


cout<<"Your smallest integer is "<<smallNumber<<endl;
cout<<"Your largest integer is "<<largeNumber<<endl;
cout<<"The product of the intergers are "<<product<<endl;
cout<<"The number 10 appeared "<<count<<" times"<<endl;

return 0;
}




I hope this helps your understanding of the language and general programming practices, not just a quick way to get your homework done

StrongSad
01-03-04, 10:10
what language? I can do the java thing..... :)

Lucid Dream
01-03-04, 10:26
Originally posted by StrongSad
what language? I can do the java thing..... :)

Hes proggramming in c++

hehe, im in java atm as well btw lol, i love it :)

Psyco Groupie
01-03-04, 10:26
looks like C not java o_O

Varaem
01-03-04, 11:22
it's not looping because in your for loop, you use i. i isn't declared anywhere. try write for (int i=0;i<5;i++)

and no, you're not writing the program correctly.

mortuuslupuss or whatever has the correct program, except he misspelt product near the end, he wrote procuct. *shrug*

Edit: I'd think vidgamer is in a college course, since there isn't a C++ advanced placement exam in HS anymore, it's Java now.

Kalamazoo
01-03-04, 11:42
This king of school work isn't about teaching you a language, it is about finding basic information about it, using this information and, maybe, basic procedural programming.

If the Neocron forum is the only way for you to find information about programming matters, honestly, your assignment is a failure.
This kind of matter is covered in any and all tutorials, many of them online. The syntax and the underlying principles aren't too different, be it in C, C++, Java or C# for this kind of matter (numeric variables declaration, looping and conditionnal branching).

You can find a nice tutorial here http://www.cplusplus.com/doc/tutorial/tut2-1.html
(found by googling for "C++ tutorial").
You may also want to try to give a look to your compiler documentation. I guess that if you are using neocron you will get something a little bit user-friendlier than 'man gcc".

No offense, but I think that you are learning your first language, and I know from personnal experience that this kind of answer will help you more than a ready made solution.

HTH

Mighty Max
01-03-04, 12:07
int product;
[...]

for(int i=0; i<25; i++)
{
[...]
product = number * procuct;
}




That wont work. change the declaration to


int product = 1 ;

to get it working. else product is 0 and anything * 0 = 0. Product therefor would never change.

Varaem
01-03-04, 12:09
Originally posted by Mighty Max
That wont work. change the declaration to


int product = 1 ;

to get it working. else product is 0 and anything * 0 = 0. Product therefor would never change.

Actually.. product is declared to a random int between INT_MIN and INT_MAX, not 0. C++ is weird like that.

Mighty Max
01-03-04, 12:29
Thats right. But since that is an windows environment there aren't that much of possible values product could be.

We have a managed environment, where even stack,heap and variable-blocks gets initialised (by 0x00,0xCC or 0xCD) so i'd assume the 0. (Windows/VCpp .data Section)

But. even tho it could be random under different compilers (yeah int product ; creates a non defined result) it is most likely that you get a wrong result by the chance of 99,99847412109375 % :p (edit: 1 of 65536 possibles is working right)

Darkborg
01-03-04, 13:37
i read the product statement on the other side and was just about to go in here and ask could that be right.
But oh well i see MM and varaem are already all over it hehe.( too late again damn)

MortuusLupus
01-03-04, 13:39
Originally posted by Mighty Max


int product = 1 ;



Agh, you're right! I got served!

joran420
01-03-04, 20:13
yeah i got it backwards should be

product=*number

Max Jefferson
01-03-04, 21:34
Originally posted by joran420
yeah i got it backwards should be

product=*number I think you'll find *= is right.

joran420
02-03-04, 00:21
lol ok then i had it right at first ... he said it didnt work so i figured i had it backwards i can never remember that syntax



but yeah you arent learning anything by asking a forum to do your homework for you....whats gonna happen when its test time and you have to take it without internet

Cruzbroker
02-03-04, 00:45
Heh, I learned (mirc) by doing something my own and reading other scripts code. Not any tutorials, I hate tutorials. Though they are useful too.

What I would like to have is command list or somekind. A List where is ever meaning of "i", "*=" "!=" "for" etc. (I've known them all. Not anymore.)
The list would help SOOOO much, but I can't find one..

Also I would like to know how the hell you mod something like Winamp etc. (for example mp3 script (DLL) between mirc <-> winamp).. All I get from decoding (?) a DLL or anything is: Wrong version, unable to open: broken. etc.

uh oh..
Giving the complete stuff doesn't really help. Explaining works.