简体   繁体   中英

Vectors, Pointers, Classes and EoF Loops (C++)

I am trying to get a vector to store objects of class 'Complex'.

This is how I have tried to get it to store:

ifstream values;
values.open("h://values.txt");

if(!values)
{
    cout<<"Error: cannot open "<<"values.txt"<<endl<<endl;
}
else 
{

    //Initialise list
    vector<Complex> v;
    Complex *c1;

    double a,b,d=0,e=0;
    char c;

    int count=0;    



    while(values)
    {
        values>>c>>a>>b;
        c1=new Complex;

        v.push_back(*c1);

        cout<<c<<"  "<<a<<" "<<b<<endl;


        switch (c)
            {
                case 'r':
                case 'R':
                case 'p':
                case 'P':
                    {
                        //Call constructor

                        v[count].setType(c);  


                        switch (c)
                        {
                            case 'r':
                            case 'R':
                                {   v[count].setReal(a);
                                    v[count].setImaginary(b);
                                    v[count].topolar(a,b,d,e);
                                    break;
                                }
                            case 'p':
                            case'P':
                                {   v[count].setLength(a);
                                    v[count].setAngle(b);
                                    v[count].frompolar(d,e,a,b);
                                    break;
                                }
                            default:
                                {   cout<<"Type Error"<<endl;
                                    break;
                                }
                        }

                        count++;
                        break;
                    }

                default:
                    {
                        //error message
                        cout<<" Failed input type, ensure all of type 'r' or 'p'"<< endl;
                        cout<<"Programme Closing"<<endl;
                        break;
                    }


            };
    }

While this will read the info in my programme, it insists on reading the last line twice (I put the cout into this loop so it was easier to see what numbers were where). I have tried using a for loop, but because I want it to run till the end of file I think I have to use a while loop, but I might be wrong.

My supervisor said something along the lines of c1 being overwritten on every loop, but I thought this should be OK on the basis the information is passed to the vector before it is overwritten by the next line so I'm a bit confused.

The next problem is that when I then try and print out all the information again, outside of the loop shown above (for example to allow for manipulation before printing) it prints the same thing over and over until the for loop reaches the count...

  int y;
int z;
while(y!=3)
{
    cout<< " What would you like to do?"<<endl;
    cout<< " Type the number of the option you would like"<<endl;
    cout<< " 1. Show all numbers in polar form"<<endl;
    cout<< " 2. Show all numbers in rectangular form"<<endl;
    cout<< " 3. Show all numbers in both forms"<<endl;
    cout<< " 4. Convert a number to its conjugate"<<endl;
    cout<< " 5. Exit"<<endl;
    cin>>y;

    switch(y)
    {
    case 1:
        for(z=0; z<count;z++)
        { 
            v[z].getLength();
            v[z].getAngle();
        cout<< a<<"   "<<b<<endl;};
        break;

    case 2:
        for (z=0; z!=count;z++)
        { 
            v[z].getReal();
            v[z].getImaginary();


        };
        break;

    case 3:
        cout<<" Real    Imaginary     |   Length    Angle   |  Original Type"<<endl;

        for(z=0; z!=count;z++)
        { v[z].getLength();
          v[z].getAngle();
                         v[z].getReal();
                    v[z].getImaginary();
                        cout<<a<<"     "<<b<<"     "<<d<<"       "<<e<<endl;

In case any of you try to run the programme:

#include "Class definitions.h"
#include <iostream>
#include <fstream>
#include <iomanip> 
#include <string.h>
#include <cmath>
#include <vector>
using namespace std;

Any help will be greatly appreciated!!

Thanks muchly! H x

Whether input succeeds is only known after the input, not before. After you do

values>>c>>a>>b;

you use the values, even if the input has failed (eg because of end of file). Put the test in the condition of the while loop:

while ( values >> c >> a >> b )

and your code should work. (Whether this is the best way to handle the problem is another question. I'd probably use std::getline() , followed by std::istringstream to parse the line I'd read. Much easier error recovery.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM