0001 Bread Bakery 35.00
0002 Soap Hygiene 20.00
0003 Milk Dairy 40.00
0004 Apples Fruit 30.00
0005 Gelatin Snacks 15.00
0006 Candy Snacks 25.00
0007 Catsup Condiments 40.00

and so im trying to read this grocery.txt file...i'm thinking of using an array...the code ive done would read the first line right? but i want to read as an array if it would be possible..I got an error when trying it..damn, i hate it when my groupmates dont have the effort to help me with our project...so I'm hoping for your support guys!
Note: I'm using turbo c++ 4.5

#include<fstream.h>
#include<cstring.h>
#include<math.h>
#include<conio.h>

void main()
{

 ifstream in;

 in.open("grocery.txt");
 string kw;
 string pc;
 string c;
 string pr;
 in>>kw;
 in>>pc;
 in>>c;
 in>>pr;
 in.close();

}

Recommended Answers

All 7 Replies

oh crap...it doesnt work

1) void main() is wrong. See this
2) conio.h is non-standard. You do not need it.
3) format your code properly.

i'm thinking of using an array...the code ive done would read the first line right?

Right.

but i want to read as an array if it would be possible..

It's completely possible.

I got an error when trying it.

trying what? Reading into an array? Since you don't have any arrays and you are reading only one line, you can't have an error reading an array. So we can't help you figure out what you did wrong.

okay so here its is..

#include<fstream.h>
#include<cstring.h>
void main()
{
 ifstream in;
 in.open("grocery.txt");
 const int r=4,c=7;
 string prod[r][c];
  for(r=0;r<4;r++)
    {
    for(c=0;c<7;c++) 
        {   
          in>>prod[r][c];
        }   
 in.close();
}

its like this..

And what happens?

Nevermind..but do check this out....the output gives me nothing..whats wrong with it?

#include<fstream.h>
#include<cstring.h>
#include<math.h>
#include<conio.h>

void main()
{
 int x,y;
 ifstream in;

 in.open("file.txt");
 string kw;
 in>>kw;
 in>>x;
 in>>y;

 if(kw=="sum"||kw=="plus"||kw=="total")
 {
  cout<< "The "<<kw<<" of "<<x<< " and  "<<y<<  "   is "<<x+y;
 }
 else if(kw=="difference"||kw=="minus"||kw=="subtraction")
 {
  cout<< "The "<<kw<<" of "<<x<< "and "<<y<< "is "<<x-y;
 }
 else if(kw=="times"||kw=="multiply"||kw=="product")
 {
  cout<< "The "<<kw<<" of "<<x<< "and "<<y<< "is "<<x*y;
 }
 else if(kw=="division"||kw=="divide"||kw=="quotient")
 {
  cout<< "The "<<kw<<" of "<<x<< "and "<<y<< "is "<<x/y;
 }
 else
 {
 clrscr();
 }
 in.close();

}

whats wrong with it?

See this

oops...damn...the txt file was located on a different location..so thats why...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.