0

so I've been having issues trying to output the results I requisite. I want to use the program to get the following data extracted from this txt file named "trees.txt".

armeniaca 39 3.5 white apricot orange
prunusPersica 23 6.5 pink peach WhitishYellow
PyrusMalinae 56 4.5 white pear green
serrulata 39 6.4 pink cherry red
pawpaw 33 28 white papaya yellow
nucifera 100 20 yellow coconut brown
Mangifera 98 16 yellow mango orange

So far, I declared a "Tree" class which sets and gets data for the following variables in order: string treeName, int height, double leafSize, string flower, string fruit, and string fruitColor. The problem comes with the usage of "ifstream", because my attempts to do so produces them incorrectly.

Here is the coding itself, disregarding the seperate .cpp and .h files for class Trees:

#include <iostream>
#include <fstream> //declare fstream
#include <iomanip> //declare iomanip
#include <string>
#include "Tree.h"

using namespace std;
const int rows = 7;

int main()
{
    Tree trees[7];
    ifstream treeData;
    string name_tree, flower_tree, fruit_tree, color_fruit;
    int height_tree;
    char whitespace = ' ';
    double size_leaf;
    treeData.open("trees.txt");
    int i = 0;
    while (i < 7)
    {
        treeData >> name_tree >> height_tree >> size_leaf >> flower_tree >> fruit_tree >> color_fruit;
        trees[i].setTreeName(name_tree);
        trees[i].setHeight(height_tree);
        trees[i].setLeafSize(size_leaf);
        trees[i].setFlower(flower_tree);
        trees[i].setFruit(fruit_tree);
        trees[i].setFruitColor(color_fruit);
        i++;
    }
    for (i = 0; i < 7; i++)
    {
        cout << left << trees[i].getTreeName() << setw(25) << trees[i].getHeight() << setw(10) << trees[i].getLeafSize()
        << setw(10) << trees[i].getFlower() << setw(10) << trees[i].getFruit() << setw(10) << trees[i].getFruitColor() << endl;
    }
    return 0;
}

The results are shown here:

armeniaca39                       3.5       white     apricot   orange
prunusPersica23                       6.5       pink      peach     WhitishYellow
PyrusMalinae56                       4.5       white     pear      green
serrulata39                       6.4       pink      cherry    red
pawpaw33                       28        white     papaya    yellow
nucifera100                      20        yellow    coconut   brown
Mangifera98                       16        yellow    mango     orange

As you can tell, the numbers ends up sticking to the words, which I don't want for my final program. Does anyone know how to solve this?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
PanchamBro
  • 31
  • 3
  • 1
    `setw` takes effect for the ***next*** formatted output operation, and not the ***previous*** one (because doing so requires the invention of a time machine). In other words, your `setw` affects the formatting of the `height` and not the `treename`. – Sam Varshavchik Apr 11 '21 at 18:30
  • Why don't you just use `setw` for the name as well? – super Apr 11 '21 at 18:31
  • @SamVarshavchik "time machine" Haha. C++ is so featured that perhaps... who knows! – Ripi2 Apr 11 '21 at 18:31
  • ahhh yeah turns out setw did mess up the formatting. Thanks @SamVarshavchik – PanchamBro Apr 11 '21 at 19:00

0 Answers0