hi, i am trying to make a weight watchers program and heres my code:

#include<iostream>
#include<fstream>
#include<conio.h>
#include<cstdio>
#include<windows.h>
using namespace std;
int main (char argc)
{
    for(;;)
    {
           system("cls");
           ifstream r;
           r.open("cdaily.txt");
           double r1;
           r >> r1;
           r.close();
           ifstream r2;
           r2.open("cweekly.txt");
           double r3;
           r2 >> r3;
           r2.close();
           cout << "You have "<<r1<<" daily points left for the day." << endl;
           cout << "You have "<<r3<<" weekly points left for the week." << endl;
           cout << "1: Eat new food (from daily) \n2: Eat new food (from weekly) \n3: Reset daily points (new day) \n4: Reset weekly points (new week) \n5: Change daily points \n6: Change weekly points \n7: Week review \n8: Exit" << endl;
           
           int z = getch();
           
           if(z == 56)
           {
                return 0;
           }
           
           if(z == 53)
           {
                system("cls");
                ofstream daily;
                daily.open("daily.txt");
                float d;
                cout << "Enter new daily points." << endl;
                cin >> d;
                daily << d;
                daily.close();
                ofstream daily1;
                daily1.open("cdaily.txt");
                daily1 << d;
                daily1.close();
                cout << "Done." << endl;
                Sleep(2000);
           }
           
           if(z == 54)
           {
                system("cls");
                ofstream weekly;
                weekly.open("weekly.txt");
                float w;
                cout << "Enter new weekly points." << endl;
                cin >> w;
                weekly << w;
                weekly.close();
                ofstream weekly1;
                weekly1.open("cweekly.txt");
                weekly1 << w;
                weekly1.close();
                cout << "Done." << endl;
                Sleep(2000);
           }
           
           if(z == 51)
           {
                ofstream daily2;
                daily2.open("cdaily.txt");
                ifstream daily3;
                daily3.open("daily.txt");
                double n2;
                daily3 >> n2;
                daily3.close();
                daily2 << n2;
                daily2.close();
                cout << "Done." << endl;
                Sleep(1500);
           }
           
           if(z == 52)
           {
                ofstream weekly2;
                weekly2.open("cweekly.txt");
                ifstream weekly3;
                weekly3.open("weekly.txt");
                double n3;
                weekly3 >> n3;
                weekly3.close();
                weekly2 << n3;
                weekly2.close();
                cout << "Done." << endl;
                Sleep(1500);
           }
           
           if(z == 49)
           {
                int l = 0;
                ofstream rday;
                rday.open("rday.txt");
                ofstream loop;
                loop.open("loop.txt");
                string f;
                double p;
                cout << "Enter the food you ate." << endl;
                getline(cin,f);
                cout << "Enter the amount of points "<<f<<" is." << endl;
                cin >> p;
                rday.seekp(0, ios::end);
                rday << "Daily food. "<< f << ". Points = "<<p;
                l++;
                loop << l;
                rday.close();
                cout << "Done." << endl;
                Sleep(1500);
           }
                
    }        
}

I havnt finished it yet but here are my problems, when i eat a new food and record it, i want the program to put everything i have eaten in a text file all in a list. Also, when i record a food i ate and go to the main menu and record another food, the program doesnt let me input the food name. Any solutions?

One, if you are trying to APPEND to these output files, I see nowhere where you specify a mode when you open the file, so you're going to be overwriting the file, not adding to the end of it. If you want to append to the end of a file, you should specify the "app"(for "append") flag when you open the file for writing.

http://www.cplusplus.com/reference/iostream/ofstream/open/

Also, when i record a food i ate and go to the main menu and record another food, the program doesnt let me input the food name

What precisely does it do? I'm suspicious that you may be mixing buffered and unbuffered reading functions and that you have a cin command that leaves a newline in the input stream, then your "getline" command grabs that newline and doesn't wait for you to type anything. Narue had a pinned thread on the subject. Can't find i now.

Solution?

Clear the input buffer before any getline functions (i.e. line 109) with the "ignore" function.

http://www.cplusplus.com/reference/iostream/istream/ignore/

Just give it a big number...

cin.ignore(80,'\n');

Stick that in front of any "get" or "getline" functions. I'm not sure how getch() works but it isn't standard, so consider using getchar() instead. Stick the "ignore" line in front of getchar() too.

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.