reading through text file

I need my code to read an example of "string:double" but reading through my code it'll give me the string but it also says it cannot be found. i can't figure out why it won't read the error on the left and right side i need to to detect wether or not the string is to the left of the : and the double to the right

#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
char c='1';

class MenuItem
{
private:
string name;
double price;
public:
/// Constructor - initialize the object from a given name and price
MenuItem(string itemName, double itemPrice);

/// Constructor - initialize the object given a single text line
/// using the syntax "itemname:price"
MenuItem(string specification);

/// Print the name and price of this menu entry
void Print();

class InvalidItem
{
private:
string message;
public:
InvalidItem(string msg)
{
message = msg;
}
string GetMessage() const
{
return message;
}
};


};
bool InterpretableAsDouble(string s);

bool InterpretableAsstring(string s);
vector<MenuItem> ProcessFile();

void MenuItem::Print()
{
cout << "the item " << name << "costs " << price <<endl;
}
MenuItem::MenuItem(string itemName, double itemPrice)
{
name=itemName;
price=itemPrice;

if (itemName == " " )
{
throw InvalidItem("there is no name");
}
else if (itemPrice <=0)
{
throw InvalidItem("this is not a real price");
}
}




MenuItem:: MenuItem(string specification)
{

double colPosition = specification.find(":");
if (colPosition > specification.length())
{

throw InvalidItem("No Semicolen found in line '" + specification + "'");
}
string leftSide = specification.substr(0, colPosition);
string rightSide = specification.substr(colPosition);
if (leftSide == "" || rightSide == "")
{
throw InvalidItem("semicolen missing in line '" + specification + "'");
}



name = atof(leftSide.c_str());
price = atof(rightSide.c_str());


double emptyPosition = specification.find(name);
if (emptyPosition > specification.length())
{

throw InvalidItem("No Menu item found in line '" + specification + "'");
}




double emptypricePosition = specification.find(price);
if (emptypricePosition > specification.length())
{

throw InvalidItem("No price item found in line '" + specification + "'");
}
string Leftside = specification.substr(0, emptypricePosition);
string Rightside = specification.substr(emptypricePosition+1);
if (Leftside == "" || Rightside == "")
{
throw InvalidItem("menu item missing price '" + specification + "'");
}
if (InterpretableAsDouble(leftSide) == false || InterpretableAsDouble(rightSide) == false)
{
throw InvalidItem("no menu item price '" + specification + "'");
}

name = atof(leftSide.c_str());
price = atof(rightSide.c_str());




}


// For doubles, use atof instead of atoi


int main()
{
try
{
vector<MenuItem> records = ProcessFile();

cout << "Here is the information:\n";
for (double i = 0; i < records.size(); i++)
{
records[i].Print();
}
}
catch (string e)
{
cout << "Sorry, an error occurred: " << e << endl;
}
catch (MenuItem::InvalidItem e)
{
cout << "Syntax error in file: " << e.GetMessage() << endl;
}

}


vector<MenuItem> ProcessFile()
{
vector<MenuItem> result;

string filename;
cout << "What is the filename? ";
getline(cin, filename);

/// Create the filestream object
ifstream fin;
/// Attempt to open a file
fin.open(filename.c_str());
/// Test for failure
if (fin.fail())
{
throw string(filename + " could not be opened.");
}

/// Read one line at a time
string line;
do
{
getline(fin, line);
if (line != "")
{
MenuItem nextMenuItem(line);
result.push_back(nextMenuItem);
}
}
while (fin.eof() == false);

/// Close the file
fin.close();

return result;
}

bool InterpretableAsDouble(string s)
{
for (int i = c-'1'; i < s.length(); i++)
{
if (!(s[i] >= 1 && s[i] <= 9))
return false;
}
return true;
}
1st. Use code tags please.
2nd. Your code is very hard to read without any formatting at all. You really need to work on your formatting...
3rd. On my compiler (Code::blocks) I couldn't even get this thing to compile until I added stdlib.
4th. around line 193, instead of
for (int i = c-'1'; i < s.length(); i++)
for (int unsigned i = c-'1'; i < s.length(); i++)
Now it compiles, no warnings, no errors... without the accompanying text file, I can't test it further.

This is better.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <iostream>
#include <vector>
#include <fstream>
#include <stdlib.h>

using namespace std;
char c='1';

class MenuItem
{
private:
    string name;
    double price;
public:
/// Constructor - initialize the object from a given name and price
    MenuItem(string itemName, double itemPrice);

/// Constructor - initialize the object given a single text line
/// using the syntax "itemname:price"
    MenuItem(string specification);

/// Print the name and price of this menu entry
    void Print();

    class InvalidItem
    {
    private:
        string message;
    public:
        InvalidItem(string msg)
        {
            message = msg;
        }
        string GetMessage() const
        {
            return message;
        }
    };


};
bool InterpretableAsDouble(string s);

bool InterpretableAsstring(string s);
vector<MenuItem> ProcessFile();

void MenuItem::Print()
{
    cout << "the item " << name << "costs " << price <<endl;
}
MenuItem::MenuItem(string itemName, double itemPrice)
{
    name=itemName;
    price=itemPrice;

    if (itemName == " " )
    {
        throw InvalidItem("there is no name");
    }
    else if (itemPrice <=0)
    {
        throw InvalidItem("this is not a real price");
    }
}




MenuItem:: MenuItem(string specification)
{

    double colPosition = specification.find(":");
    if (colPosition > specification.length())
    {

        throw InvalidItem("No Semicolen found in line '" + specification + "'");
    }
    string leftSide = specification.substr(0, colPosition);
    string rightSide = specification.substr(colPosition);
    if (leftSide == "" || rightSide == "")
    {
        throw InvalidItem("semicolen missing in line '" + specification + "'");
    }



    name = atof(leftSide.c_str());
    price = atof(rightSide.c_str());


    double emptyPosition = specification.find(name);
    if (emptyPosition > specification.length())
    {

        throw InvalidItem("No Menu item found in line '" + specification + "'");
    }




    double emptypricePosition = specification.find(price);
    if (emptypricePosition > specification.length())
    {

        throw InvalidItem("No price item found in line '" + specification + "'");
    }
    string Leftside = specification.substr(0, emptypricePosition);
    string Rightside = specification.substr(emptypricePosition+1);
    if (Leftside == "" || Rightside == "")
    {
        throw InvalidItem("menu item missing price '" + specification + "'");
    }
    if (InterpretableAsDouble(leftSide) == false || InterpretableAsDouble(rightSide) == false)
    {
        throw InvalidItem("no menu item price '" + specification + "'");
    }

    name = atof(leftSide.c_str());
    price = atof(rightSide.c_str());




}


// For doubles, use atof instead of atoi


int main()
{
    try
    {
        vector<MenuItem> records = ProcessFile();

        cout << "Here is the information:\n";
        for (double i = 0; i < records.size(); i++)
        {
            records[i].Print();
        }
    }
    catch (string e)
    {
        cout << "Sorry, an error occurred: " << e << endl;
    }
    catch (MenuItem::InvalidItem e)
    {
        cout << "Syntax error in file: " << e.GetMessage() << endl;
    }

}


vector<MenuItem> ProcessFile()
{
    vector<MenuItem> result;

    string filename;
    cout << "What is the filename? ";
    getline(cin, filename);

/// Create the filestream object
    ifstream fin;
/// Attempt to open a file
    fin.open(filename.c_str());
/// Test for failure
    if (fin.fail())
    {
        throw string(filename + " could not be opened.");
    }

/// Read one line at a time
    string line;
    do
    {
        getline(fin, line);
        if (line != "")
        {
            MenuItem nextMenuItem(line);
            result.push_back(nextMenuItem);
        }
    }
    while (fin.eof() == false);

/// Close the file
    fin.close();

    return result;
}

bool InterpretableAsDouble(string s)
{
    for (int i = c-'1'; i < s.length(); i++)
    {
        if (!(s[i] >= 1 && s[i] <= 9))
            return false;
    }
    return true;
}
sorry new to site didn't realize it posted like that...

and the text file needed is just a basic txt file

it needs input like burrito:5.5 or something like that

i am suppose to throw an error if burrito doesn't show up on the left side

or if the 5.5 doesn't show up on the right

but my code will read the file but i can't get my error to work
I need my code to read an example of "string:double"


You are using getline, which throws it all in one string. I take your instructions to mean you should read the string, then read the double...


Topic archived. No new replies allowed.