Need fo help!

B

bryant058

When I type every number=0,the largest is 4. It'sconfusing! What's
wrong with my program?
---------------------------(my source
code)-------------------------------
#include<iostream>
using namespace std;
int main()
{
int counter=0;
int largest,number;


while(counter<10)
{
cout<<"Enter a Number:";
cin>>number;
if (number>largest)
largest=number;
else
largest=largest;


counter=counter+1;


}
cout<<"The largest is "<<largest<<endl;


return 0;
}
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

#include<iostream>
using namespace std;
int main()
{
int counter=0;
int largest,number;

// Insert here:
cerr << "TESTING: value of largest is: " << largest << '\n';
 
T

terminator

When I type every number=0,the largest is 4. It'sconfusing! What's
wrong with my program?
---------------------------(my source
code)-------------------------------
#include<iostream>
using namespace std;
int main()
{
int counter=0;
int largest,number;

while(counter<10)
{
cout<<"Enter a Number:";
cin>>number;
if (number>largest)
largest=number;
else
largest=largest;

counter=counter+1;

}
cout<<"The largest is "<<largest<<endl;

return 0;



}- Hide quoted text -- Show quoted text -
int largest,number;
'largest' is not initialized and contains a random value which may be
greater than all your inputs . this is not pascal ,in c/c++ variables
of built in types are not automatically inititialized with zero.
furthur more i suggest the 'for' loop instead of 'while' .
you`d better replace your 'main' with this one:
int main()
{
int counter;
unsigned int number,largest=0;

for(counter=0;counter<10;counter++)
{
cout<<"Enter a Number:";
cin>>number;
if (number>largest)
largest=number;
};
cout<<"The largest is "<<largest<<endl;

return 0;
}-
 
B

bryant058

(e-mail address removed) ¼g¹D¡G
When I type every number=0,the largest is 4. It'sconfusing! What's
wrong with my program?
---------------------------(my source
code)-------------------------------
#include<iostream>
using namespace std;
int main()
{
int counter=0;
int largest,number;


while(counter<10)
{
cout<<"Enter a Number:";
cin>>number;
if (number>largest)
largest=number;
else
largest=largest;


counter=counter+1;


}
cout<<"The largest is "<<largest<<endl;


return 0;
}
Thanks for all your help!!^ ^
 
B

BobR

terminator wrote in message ...
'largest' is not initialized and contains a random value which may be
greater than all your inputs . this is not pascal ,in c/c++ variables
of built in types are not automatically inititialized with zero.
furthur more i suggest the 'for' loop instead of 'while' .
you`d better replace your 'main' with this one:
int main(){
int counter;
unsigned int number,largest=0;

Note 'number'
for(counter=0;counter<10;counter++)

If 'counter' is not used outside the loop, just do:

for(int counter(0); counter < 10; ++counter)
{
cout<<"Enter a Number:";
cin>>number;

The user typed in 'A', 'number' is still UB.
Initialise both 'number' and 'largest'.
// unsigned int number(0), largest(0);
 
T

terminator

BobR äæÔÊå ÇÓÊ:
terminator wrote in message ...

Note 'number'


If 'counter' is not used outside the loop, just do:

for(int counter(0); counter < 10; ++counter)


The user typed in 'A', 'number' is still UB.
Initialise both 'number' and 'largest'.
// unsigned int number(0), largest(0);
U R write I forgot that we are using cin/cout and wrote that part in C
style .
 
S

sam

Hi ,
What you are doing is you are comparing numbe with largest.
But the
int largest=0; is the declaration.
and in for loop you are not changing the contents of it.
What you are doing is you are comparing the number with 0.
if(number>largest)
So infact in order to get the value real in largest.
you must have to constantly store big value in largest and compare it
with number.
using for loop then only will get the real largest value after 10
iterations.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top