Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7654575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:17:27+00:00 2026-05-31T12:17:27+00:00

I am trying to get a vector to store objects of class ‘Complex’. This

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:17:28+00:00Added an answer on May 31, 2026 at 12:17 pm

    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 (e.g. 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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get SSE functionality in my vector class (I've rewritten it
I'm trying get values from a GridView using the following code: foreach (GridViewRow row
Trying to get my css / C# functions to look like this: body {
Trying to get this example working from http://www.munna.shatkotha.com/blog/post/2008/10/26/Light-box-effect-with-WPF.aspx However, I can't seem to get
Trying to get comfortable with jQuery and I have encountered some sample code that
Trying to get this to work, with no luck: [DataMember] public Type ParameterType {
I'm trying to use stxxl as a backing store to a vector. If I
I'm trying to get my head around solving the following problem. I have the
What I am trying to do is save a Move objects into a Vector
I'm trying to get my prof's overengineered C++ code to compile. This is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.