Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,284 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,014 people online right now. Registration is fast and FREE... Join Now!




New to Programming, NEED HELP

 
Reply to this topicStart new topic

New to Programming, NEED HELP, Simple loop problem becomes not so simple for me.

kobishmaster
6 Nov, 2008 - 08:02 PM
Post #1

New D.I.C Head
*

Joined: 6 Nov, 2008
Posts: 4

This is supposed to be a program that is supposed to read in strings from an input file and output the first word of each line. There are several ways to solve this problem. I'm supposed to solve it in two ways,
1.) once with an input function or operator,
2.) the other with one or more string functions.

cpp

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
ifstream infile;
string line, inputfilename;

cout << "Enter the file name ";
getline (cin, inputfilename);

infile.(inputfilename.c_str())

// some code needed here to input from the file

while (infile)
{

// some code here to output the desired word

// some code needed here to input from the file

}
infile.close();
return 0;
}


Example: an input file called xyz
This is the story of
a cat and a
dog who were
friends

Example Output should be:
This
a
dog
friends

I have written some code for this, but I can't ever seem to get out of the loop. If anyone can help, it would be appreciated. I've also got another similar problem:

This program should read in 6 numbers from the keyboard and then indicate whether any of them were larger than 500 or smaller than 10. It is possible that both events occurred, that neither did or that only one of the two did. And it is possible that the same event happens more than once.
First, write the program so that it reads in 6 numbers and echoes them to the screen. You must use a loop.
Then add in a flag that will detect numbers larger than 500. Use a bool variable.
Then add in another flag that will detect numbers smaller than 10. Again use a bool variable.
The feedback to the user should happen following (outside) the end of the loop, not during the input phase. The user is not interested in how many times an event occurred, only whether it happened or not.

Here is the code I've written, and like most of the things I do in C++, it doesn't work.

#include <iostream>
using namespace std;

int main()
{
int a,b,c,d,e;
bool over500 = false; //Bool flag for testing if numbers are over 500
bool under10 = false; //Bool flag for testing if numbers are under 10

// Displays command for user to enter input of integers then reads into variable location
cout<<"Enter the first number: "<<endl;
cin >> a;
cout<<"Enter the second number: "<<endl;
cin >> b;
cout<<"Enter the third number: "<<endl;
cin >> c;
cout<<"Enter the fourth number: "<<endl;
cin >> d;
cout<<"Enter the fifth number: "<<endl;
cin >> e;

while (a)
{
if (a > 500 || b > 500 || c > 500 || d > 500 || e > 500 )
over500 = true;
if (a < 10 || b < 10 || c < 10 || d < 10 || e < 10 )
under10 = true;
}
if (over500 == true)
cout << "One of the numbers is greater than 500. "<<endl;
if (under10 ==true)
cout << "One of the numbers is less than 10. "<<endl;
return 0;
}


Again, anyone that help me out with these problems, it would be greatly appreciated. Thanks.

** Edit ** code.gif

User is offlineProfile CardPM
+Quote Post


muballitmitte
RE: New To Programming, NEED HELP
6 Nov, 2008 - 09:58 PM
Post #2

D.I.C Head
Group Icon

Joined: 5 Nov, 2008
Posts: 81



Thanked: 9 times
Dream Kudos: 75
My Contributions
Your problems are clearly these two lines: you never get out of the loops
CODE

while (infile)
while (a)


The main idea is
CODE

while(condition_is_true)
do_stuff;
somehow_negate_condition_so_you_can_get_Out_of_loop
end while


so in your case

CODE

while(not EOF)
readline from file
end while


and

CODE

while(a)
{
  cin >> a; read 0 to exit -although a bad idea
  ....
}

User is offlineProfile CardPM
+Quote Post

David W
RE: New To Programming, NEED HELP
8 Nov, 2008 - 02:30 AM
Post #3

D.I.C Addict
Group Icon

Joined: 20 Sep, 2008
Posts: 696



Thanked: 62 times
Dream Kudos: 425
My Contributions
This may help to start ... biggrin.gif

cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
ifstream fin, fin2;
string word, line, inputfilename;

cout << "Enter the file name: ";
getline (cin, inputfilename);

fin.open(inputfilename.c_str());

// way 1 ....
while( getline(fin,line) )
{
int i=0;
while(!(line[i]==0 || line[i]==' ')) ++i;
cout << line.substr(0,i)<<endl;
}
fin.close();
system("pause");

// way 2 ...
fin2.open(inputfilename.c_str());
while( fin2>>word )
{
cout << word << endl;
getline(fin2, line);
}
fin2.close();
system("pause");

return 0;
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 7/4/09 02:58PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month