Re: lowest number



 DEVELOP > c-Plus-Plus > Re: lowest number

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Randi"
Date: 11 Nov 2003 10:23:59 PM
Object: Re: lowest number
man, am I tired
sorry bout a the posts.
# include <iostream>
using namespace std;
void main ()
{
int lowest = 0;
int number = 0;
cout<<"Enter in the number"<<endl;
cin>>number;
lowest = number;
for(int count =0; count < 3; count++)
{
cout<<"Enter in the number"<<endl;
cin>>number;
if(number < lowest)
lowest = number;
}
cout<<lowest;
}
"Randi" <RSaddler@stny.rr.com> wrote in message news:...

Whoa there,
I am not a kid trying to get an answer for an assignment. I am a 38 year
old student taking my required C++ class for my IT degree. I am also a
member of Phi Theta Cappa with a 3.8 GPA, trying to study for an exam
tomorrow. These are practice problems to help us study. Here is my code
for the for loop. How would you make this work taking input from a file.

Thanks,
Kelsey

"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:bohsb.128304$9E1.638773@attbi_s52...

"Randi" <RSaddler@stny.rr.com> wrote...

I am having problems with this question: Write a C++ while statement

that

will input numbers from a file until the end of file is reached.

Output

the

lowest number in the file.


There is no question here. It looks much more like an assignment.
Have you tried asking your teacher about it?

Ok, I do understand the logic, I believe the following for loop solves

it

from keyboard input.


Which "following for loop" are you talking about?

But how do you point to any given value from a file.
You can't assign anything to a variable. Little lost on this one.


To input something from a file you need to open a file. Read about
ifstream class in your textbook.

Victor





.

User: "Victor Bazarov"

Title: Re: lowest number 11 Nov 2003 10:39:20 PM
"Randi" <RSaddler@stny.rr.com> wrote...

man, am I tired
sorry bout a the posts.

# include <iostream>
using namespace std;
void main ()

{
int lowest = 0;
int number = 0;

cout<<"Enter in the number"<<endl;
cin>>number;
lowest = number;
for(int count =0; count < 3; count++)
{
cout<<"Enter in the number"<<endl;
cin>>number;
if(number < lowest)
lowest = number;
}

cout<<lowest;

}

OK. This seems a bit better.
Now, instead of 'cin' you need an object of type 'ifstream'.
In order to get it, you need to include <fstream>. Example:
#include <iostream>
#include <fstream>
using namespace std;
int main() // yes, 'int' not 'void'
{
ifstream fin("filename");
int number = 0;
fin >> number;
int lowest = number;
cout << "Lowest = " << lowest << endl;
return 0;
}
As you can see, there are no prompts to enter the number
(since the file is just read without any delay, or Enter key
pressed).
I trust you could extend this example to read more than one
number from the file using a loop.
Victor
.

User: "Robert W Hand"

Title: Re: lowest number 12 Nov 2003 04:08:03 AM
On Wed, 12 Nov 2003 04:23:59 GMT, "Randi" <RSaddler@stny.rr.com>
wrote:

# include <iostream>
using namespace std;
void main ()

int main()


{
int lowest = 0;
int number = 0;

cout<<"Enter in the number"<<endl;
cin>>number;
lowest = number;

int lowest = number;
In C++, you can declare lowest when you need it.

for(int count =0; count < 3; count++)

I would avoid magic numbers.

{
cout<<"Enter in the number"<<endl;
cin>>number;
if(number < lowest)
lowest = number;
}

cout<<lowest;

I did not see any file handling code. You need to use fstream. There
is no error handling attempt in the code.

}
"Randi" <RSaddler@stny.rr.com> wrote in message news:...

Whoa there,
I am not a kid trying to get an answer for an assignment. I am a 38 year
old student taking my required C++ class for my IT degree. I am also a
member of Phi Theta Cappa with a 3.8 GPA, trying to study for an exam

Isn't it Phi Beta Kappa? :-)
--
Best wishes,
Bob
.

User: "Karl Heinz Buchegger"

Title: Re: lowest number 12 Nov 2003 03:28:36 AM
Randi wrote:


man, am I tired
sorry bout a the posts.

# include <iostream>
using namespace std;
void main ()

{
int lowest = 0;
int number = 0;

cout<<"Enter in the number"<<endl;
cin>>number;
lowest = number;
for(int count =0; count < 3; count++)
{
cout<<"Enter in the number"<<endl;
cin>>number;
if(number < lowest)
lowest = number;
}

cout<<lowest;

}

I still don't see any file handling in the above.
Even if you are a member of Phi Theta Cappa with a 3.8 GPA (whatever
that may be), you need to
open a file
as long as a number could be read from the file
process that number
output the result
Someone with a 3.8 GPA should be able to find the details in his textbook :-)
--
Karl Heinz Buchegger
kbuchegg@gascad.at
.
User: "Chris Theis"

Title: Re: lowest number 12 Nov 2003 06:33:14 AM
"Karl Heinz Buchegger" <kbuchegg@gascad.at> wrote in message
news:3FB1FD44.273A240D@gascad.at...



Randi wrote:


man, am I tired
sorry bout a the posts.

# include <iostream>
using namespace std;
void main ()

{
int lowest = 0;
int number = 0;

cout<<"Enter in the number"<<endl;
cin>>number;
lowest = number;
for(int count =0; count < 3; count++)
{
cout<<"Enter in the number"<<endl;
cin>>number;
if(number < lowest)
lowest = number;
}

cout<<lowest;

}


I still don't see any file handling in the above.
Even if you are a member of Phi Theta Cappa with a 3.8 GPA (whatever
that may be), you need to

To the OP:
Please don't tell me you got your 3.8 GPA taking IT courses?


open a file

as long as a number could be read from the file
process that number

output the result

Someone with a 3.8 GPA should be able to find the details in his textbook

:-)


Especially if she/he is a member of Phi Theta Cappa! (wasn't that some
mysterious secret society of IT gurus?) :-)
Okay, I'll drop my sarcasm :-) and try to give the OP some hints.
1. open a file - check out the syntax of the ifstream class
2. Verify that opening the file was successful!
3. read the file until the end is reached - for this a while loop comes in
handy. Using it in combination with the stream extractor operator >> you can
do wonderful things!
4. within the while loop try to find the lowest number so far (this should
be really easy!)
5. output the result
Any decent C++ beginners (even if you have a GPA of 3.8) book should have
something like this hidden somewhere in the first chapters.
Chris
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER