// MajorProJectMB.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include<cstring>
#include<fstream>
#include<iostream>
#include<string>
#include <iomanip>
#include <cstdlib>
#include <string>
using std::ifstream;
using std::ofstream;
using std::ios;
using namespace std;
struct project
{
    char comma[10];
    double NumFields[10];
    string lastfieldName;
    string firstfieldName;
    double Toltal = 0;
};
void getfile(project Fields[8]);
void bubbleSort(project Fields[8], double NumFields[10]);
void swap(double *xp, double *yp);

void outputfile(project Fields[8]);

int main()
{
    project Fields[8];
    getfile(Fields);
    outputfile(Fields);
    system("PAUSE");
    return 0;
}

void getfile(project Fields[8])
{

    ifstream inStream;
    ofstream outStream;
    int count = 0;
    inStream.open("Project-Input.dat");

    if (inStream.fail())
    {
        cerr << "File not located!";
        system("PAUSE");
        exit(1);//ifstream Fieldin;;

    }
    else
    {
        while (inStream.good())
        {

            for (int loop = 0; loop < 8; loop++)
            {

                inStream >> Fields[loop].lastfieldName >> Fields[loop].firstfieldName >> Fields[loop].comma[0] >> Fields[loop].NumFields[0] >> Fields[loop].comma[1] >> Fields[loop].NumFields[1] >> Fields[loop].comma[2] >> Fields[loop].NumFields[2] >> Fields[loop].comma[3] >> Fields[loop].NumFields[3] >> Fields[loop].comma[4] >> Fields[loop].NumFields[4] >> Fields[loop].comma[5] >> Fields[loop].NumFields[5] >> Fields[loop].comma[6] >> Fields[loop].NumFields[6] >> Fields[loop].comma[7] >> Fields[loop].NumFields[7] >> Fields[loop].comma[8] >> Fields[loop].NumFields[8] >> Fields[loop].comma[9] >> Fields[loop].NumFields[9];

            }

        }
        /*for (int loop = 0; loop < 8; loop++)
        {
        for (int count=0; count<10; count++)
        {
        Fields[loop].Toltal = Fields[loop].Toltal + Fields[loop].NumFields[count];
        }
        }*/

        //inStream.close();
        //outStream.close();

        //bubbleSort(Fields, NumFields);
        //outputfile(completeField);

    }
}

void outputfile(project Fields[8])
{
    ifstream inStream;
    ofstream outStream;
    string Oline;
    int loop = 0;
    outStream.open("Project-Output.csv");
    if (outStream.fail())
    {
        cerr << "File not located!";
        system("PAUSE");
        exit(1);//ifstream Fieldin;

    }
    else
    {
        while (loop<8)
        {
            outStream << Fields[loop].lastfieldName << " " << Fields[loop].firstfieldName << Fields[loop].comma[0] << Fields[loop].NumFields[0] << Fields[loop].comma[1] << Fields[loop].NumFields[1] << Fields[loop].comma[2] << Fields[loop].NumFields[2] << Fields[loop].comma[3] << Fields[loop].NumFields[3] << Fields[loop].comma[4] << Fields[loop].NumFields[4] << Fields[loop].comma[5] << Fields[loop].NumFields[5] << Fields[loop].comma[6] << Fields[loop].NumFields[6] << Fields[loop].comma[7] << Fields[loop].NumFields[7] << Fields[loop].comma[8] << Fields[loop].NumFields[8] << Fields[loop].comma[9] << Fields[loop].NumFields[9] << endl;
            loop++;
        }
        inStream.close();
        outStream.close();

    }

}

so it iterates through the first line and prints it to the file but then i get this:
Doe Suzie,100,95,85,100,70,65,100,95,70,96S-9.25596e+61Ì-9.25596e+61Ì-9.25596e+61Ì-9.25596e+61Ì-9.25596e+61Ì-9.25596e+61Ì-9.25596e+61Ì-9.25596e+61Ì-9.25596e+61Ì-9.25596e+61

and the garbage continues for the rest of the 8 lines, im using c++ what am i doing wrong?

Recommended Answers

All 9 Replies

You have over 100 lines of uncommented code. There may be files involved. Why not use a nice IDE you can debug in? I'm running into students that attempt to use the command line and come back worse for wear.

yeah, thats what i said im attemting to read information from a file and attemting to read it into an array fields, and then output it to a .csv file, and im using a struct to call upon my variables for for the names, commas and numbers:

file contains this:
Doe Suzie,100,95,85,100,70,65,100,95,70,96
Santos Judy,60,65,80,75,85,75,70,80,83,66
Louis Mario,80,95,85,75,65,80,100,95,97,83
Smith John,85,100,85,80,80,80,85,95,67,76
Brown Maurice,70,100,90,85,70,95,70,95,53,67
Smith Joe,60,65,85,65,90,85,65,60,87,47
Dells Vicki,75,70,85,85,100,80,65,55,93,87
Welch Harriott,90,95,70,100,95,80,75,0,73,100

struct project//this is my struct where i hold all my variables
{
    char comma[10];//an array for all the commas 
    double NumFields[10];//an array for all the numbers
    string lastfieldName;// variable to hold the one of the names
    string firstfieldName;//variable to hold the other name

    double Toltal = 0;// i am meant to add up all the numfields and place the total at end of each line but havent added //that in since im stuck with all the garbage data
};

void getfile(project Fields[8])
{

    ifstream inStream;
    ofstream outStream;

    inStream.open("Project-Input.dat");//opens my file

    if (inStream.fail())//checks if it opens properly
    {
        cerr << "File not located!";
        system("PAUSE");
        exit(1);//ifstream Fieldin;;

    }
    else
    {
        while (inStream.good())
        {

            for (int loop = 0; loop < 8; loop++)
            {

                inStream >> Fields[loop].lastfieldName >> Fields[loop].firstfieldName >> Fields[loop].comma[0] >> Fields[loop].NumFields[0] >> Fields[loop].comma[1] >> Fields[loop].NumFields[1] >> Fields[loop].comma[2] >> Fields[loop].NumFields[2] >> Fields[loop].comma[3] >> Fields[loop].NumFields[3] >> Fields[loop].comma[4] >> Fields[loop].NumFields[4] >> Fields[loop].comma[5] >> Fields[loop].NumFields[5] >> Fields[loop].comma[6] >> Fields[loop].NumFields[6] >> Fields[loop].comma[7] >> Fields[loop].NumFields[7] >> Fields[loop].comma[8] >> Fields[loop].NumFields[8] >> Fields[loop].comma[9] >> Fields[loop].NumFields[9];
                //the line of code above starts at 0 for a loop and is meant to iterate through the file checking to see which is there, character, double, or string and adds it to the instream, and then its meant to do that for 8 lines of data in the file
            }

    }
}
void outputfile(project Fields[8])//my output function to write the data to new file
{
    ifstream inStream;
    ofstream outStream;
    string Oline;
    int loop = 0;
    outStream.open("Project-Output.csv");//creates a new file
    if (outStream.fail())//checks if it loads, it does
    {
        cerr << "File not located!";
        system("PAUSE");
        exit(1);//ifstream Fieldin;

    }
    else
    {
        while (loop<8)// loop for out puting the data
        {
            outStream << Fields[loop].lastfieldName << " " << Fields[loop].firstfieldName << Fields[loop].comma[0] << Fields[loop].NumFields[0] << Fields[loop].comma[1] << Fields[loop].NumFields[1] << Fields[loop].comma[2] << Fields[loop].NumFields[2] << Fields[loop].comma[3] << Fields[loop].NumFields[3] << Fields[loop].comma[4] << Fields[loop].NumFields[4] << Fields[loop].comma[5] << Fields[loop].NumFields[5] << Fields[loop].comma[6] << Fields[loop].NumFields[6] << Fields[loop].comma[7] << Fields[loop].NumFields[7] << Fields[loop].comma[8] << Fields[loop].NumFields[8] << Fields[loop].comma[9] << Fields[loop].NumFields[9] << endl;
            loop++;// takes the data i read and puts it into the new file

        }
        inStream.close();
        outStream.close();

    }

}

however i think something is wrong with my inStream, like somehow im going out of bounds or something. Im not really sure what you meant use a nice IDE and all that, im working in visual studio. do i need more comments than this or to explain it better?

its basically not puting anything in my instream after a certain point

  • Fields 0x0039f1c0 {comma=0x0039f1c0 "ÌÌÌÌÌÌÌÌÌÌ... NumFields=0x0039f1d0 {-9.2559631349317831e+61, -9.2559631349317831e+61, ...} ...} project *
  • Fields[loop] {comma=0x0039f1c0 "ÌÌÌÌÌÌÌÌÌÌ... NumFields=0x0039f1d0 {-9.2559631349317831e+61, -9.2559631349317831e+61, ...} ...} project
  • Fields[loop].NumFields 0x0039f1d0 {-9.2559631349317831e+61, -9.2559631349317831e+61, -9.2559631349317831e+61, -9.2559631349317831e+61, ...} double[10]
    Fields[loop].NumFields[0] -9.2559631349317831e+61 double
    Fields[loop].NumFields[1] -9.2559631349317831e+61 double
    Fields[loop].NumFields[2] -9.2559631349317831e+61 double
    Fields[loop].NumFields[3] -9.2559631349317831e+61 double
    Fields[loop].NumFields[4] -9.2559631349317831e+61 double
    Fields[loop].NumFields[5] -9.2559631349317831e+61 double
    Fields[loop].NumFields[6] -9.2559631349317831e+61 double
    Fields[loop].NumFields[7] -9.2559631349317831e+61 double
    Fields[loop].NumFields[8] -9.2559631349317831e+61 double
    Fields[loop].NumFields[9] -9.2559631349317831e+61 double
  • Fields[loop].comma 0x0039f1c0 "ÌÌÌÌÌÌÌÌÌÌ... char[10]
    Fields[loop].comma[0] -52 'Ì' char
    Fields[loop].comma[1] -52 'Ì' char
    Fields[loop].comma[2] -52 'Ì' char
    Fields[loop].comma[3] -52 'Ì' char
    Fields[loop].comma[4] -52 'Ì' char
    Fields[loop].comma[5] -52 'Ì' char
    Fields[loop].comma[6] -52 'Ì' char
    Fields[loop].comma[7] -52 'Ì' char
    Fields[loop].comma[8] -52 'Ì' char
    Fields[loop].comma[9] -52 'Ì' char
  • Fields[loop].firstfieldName "" std::basic_string<char,std::char_traits,std::allocator >
  • Fields[loop].lastfieldName "" std::basic_string<char,std::char_traits,std::allocator >
  • inStream {_Filebuffer={_Pcvt=0x00000000 _Mychar=0 '\0' _Wrotesome=false ...} } std::basic_ifstream<char,std::char_traits >
    loop 0 int

idk if that makes any sense to you cause it doesnt to me

"its basically not puting anything in my instream after a certain point"

So it works for a few lines? Or breaks on the first attempt?

I have to repeat that VS is very nice about letting us set breaks so we don't have to wait till your loop at line 31 completes. We can pause it after 34 and look at what it picks up, what the value of loop is etc.

Then again, why not use a csvreader as there are many fine ones out there today.

it breaks after the first pass/attemt also its my assingment to put the data from a .dat file into an array and then write it to a csv file, not sure how a csv reader would help with that, probably cause i never heard of it before or use one before

i wouldn't know were to start with that

file contains this:
Doe Suzie,100,95,85,100,70,65,100,95,70,96

That looks like a CSV file to me. But everyone starts somewhere. That is, the file extension might be .DAT or .CSV but does not matter as we get to read a line, parse it and so on. Your idea of taking the line without parsing is well, you can see how that's working out. Time to read how it's done and implement it for your code.

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.