Task for school

Hello everyone,my teacher gave me following task:Write a program which will create a ellipse with sizes writen in text document.So for example the sizes in text document are writen like this
40 //x1
40 //y1
80 //x2
80 //y2

and the program will load the values and create ellipse with the sizes
help guys so far ive been able to come up with this but the teacher says it aint good enough :/

float a,b,c,d;
ifstream f;
f.open("ellipse.txt");
f.get(a);
f<<"\n";
f.get(b);
f<<"\n";
f.get(c);
f<<"\n";
f.get(d);
f.close()
Image1->Canvas->Ellipse(a,b,c,d);



Last edited on
Did you ever bother to compile ?
Do you have an idea what get actually reads ?
Why do you think you can write into an ifstream ?
Thanks for reply thomas
Im not trying to write,im trying to load the values (they are already writen in .txt document) and no i just dont know how to get/load these values ,i tried running the program but it never load the values so it cant create ellipse :(
Last edited on
>> operator reads from an input stream into a variable, delimited by whitespace.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include <fstream>
#include <iostream>

int main()
{
    std::ifstream f("ellipse.txt");
    
    double a, b, c, d;
    if (f >> a >> b >> c >> d)
    {
        std::cout << a << " " << b << " " << c << " " << d << "\n";
    }
    else
    {
        std::cout << "failed to read\n";
    }
}
Last edited on
Ganado's version reads files, like
40  40 80
    80

or
40
40
80
80

but if your file really contains:
40 //x1
40 //y1
80 //x2
80 //y2

then you have to read a number and discard the rest of line (//...)
I just assumed the instructor put the "comments" there just to say what each number actually represented, and that they weren't actually in the file. But true, if that's exactly how the file looks, it would be something like:

1
2
3
4
5
6
7
8
9
double a, b, c, d;
std::string ignore;

if (f >> a >> ignore
      >> b >> ignore
      >> c >> ignore
      >> d >> ignore)
{  /* ... */    } 
  
Im not trying to write
f<<"\n";
What is his supposed to do?
<< is the normal output operator
Ganado thanks a lot man for your post,thats exactly what i needed also the x1,x2 was just my comments they wasnt actually in writen in file but usefull post aswell,cheers
Topic archived. No new replies allowed.