storing values.....NEED HELP.

This is a discussion on storing values.....NEED HELP. within the C++ Programming forums, part of the General Programming Boards category; How would i go about storing a value, and everytime that i open my program, it would load that value? ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    8

    storing values.....NEED HELP.

    How would i go about storing a value, and everytime that i open my program, it would load that value?

    sry, i just started learning the language.... and i didnt really see anything in the tutorials that would have helped me. Unless maybe i overlooked a tutorial.

    Also, does anyone know a tutorial that teachs you to make a database in c++?
    Last edited by new2tehgame; 09-14-2005 at 05:46 PM.

  2. #2
    Registered User Boomba's Avatar
    Join Date
    Jun 2003
    Posts
    89
    to store a value you use a variable...you should start from the beginning of the tutorials and work your way up...the value wont get loaded again automatically unless you store tha value in a file on your hard drive and load from there...once the pogram closes its memory in ram are cleared there for you use the HD for storage.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    The basic way to do that is to store the value in a file. Learn how to use fstreams, which are not much different than cin and cout.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    Quote Originally Posted by Daved
    The basic way to do that is to store the value in a file. Learn how to use fstreams, which are not much different than cin and cout.
    I know how to do that stuff... and i have read the tutorials.

    But i left out that i wanted the variable to be forever changing. Based on user input.
    Last edited by new2tehgame; 09-14-2005 at 06:52 PM. Reason: typo = \

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    Code:
    #include "data2.h"
    
    int main()
    	{
    	clrscr();
    		{
    	if(V.p_bal == 0)
    		{
    		cout<<"Please input a starting balance.\n";
    		cout<<">";
    		cin>>V.p_bal;
    		clrscr();
    		}
    	
    	else
    	if(V.p_bal != 0)		
    			{
    			cout<<"Your previous balance was "<<V.p_bal<<"\n";
    			cout<<"\nPress Enter to continue...\n";
    			cin.get();
    			cin.get();
    			clrscr();
    			}
    			char exit = 'n';
    			while(exit != 'y')
    				{
    			MENU();
    			switch(V.sel)
    				{
    				case 0 :
    				exit = 'y';
    				break;
    				
    				case 1 :
    				cout<<"Insert a value to be added to "<<V.p_bal<<"\n";
    				cout<<">";
    				cin>>V.add;
    				clrscr();
    				V.total = V.p_bal + V.add;
    				cout<<"Your new total is "<<V.total<<"\n";
    				cout<<"\nPress any key to continue...";
    				cin.get();
    				cin.get();
    				break;
    				
    				case 2 :
    				cout<<"Insert a value to be subtracted from "<<V.p_bal<<"\n";
    				cout<<">";
    				cin>>V.sub;
    				clrscr();
    				V.total = V.p_bal - V.sub;
    				cout<<"Your new total is "<<V.total<<"\n";
    				cout<<"\n Press any key to continue...";
    				cin.get();
    				cin.get();
    				break;
    				}
    				clrscr();
    				V.p_bal = V.total;
    				
    }
    }
    V.p_bal = V.total;
    }
    					
    void MENU()
    	{
    	cout<<"\t..[]-MENU-[]..\n";
    	cout<<"\n(1) Add to "<<V.p_bal<<".\n";
    	cout<<"(2) Subtract from "<<V.p_bal<<".\n";
    	cout<<"(0) Exit\n";
    	cin>>V.sel;
    	}
    void clrscr()
    	{
    	COORD coordScreen = { 0, 0 };
        DWORD cCharsWritten;
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        DWORD dwConSize;
        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    	FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
        SetConsoleCursorPosition(hConsole, coordScreen);
        }
    This is my main file.... if you need me to post the header file then i can.

    And i realize some of that stuff isn't needed.... i will clean it up once i have it complete, it looks like that because i was prac dif code.

    But any tips would be awsome, i just started learnin this 3 days ago.
    Last edited by new2tehgame; 09-14-2005 at 06:53 PM. Reason: typo =\

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    I don't see anything in there about fstreams (file streams). Your program should open the file as an input stream and read in the value you want to save. If the file doesn't exist then start with a default value (like 0). After the user has made the changes to the balance, you can open the file again as an output stream and write out the new balance (or whatever value you want to save). The next time the program is run the new data should be in the file.

    You can make it more advanced by writing other things to the file, just remember to write them in a specific order and read them in using the same order. To make it easy, you should just read in the whole file at the beginning and save the information while the program is running, then re-write the whole file from the start when the program ends.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    thx for the help, and for puttin up with my stupidity.... i had used the fstream before but i guess i messed it up.... i rewrote it and now it works.

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    Code:
    #include "data2.h"
    
    int main()
    	{
    	ifstream bal_i ("balance.cpp");
    	bal_i>>V.p_bal;
    	bal_i.close();
    	ofstream bal_o ("balance.cpp");
    	clrscr();
    		{
    	if(V.p_bal == 0)
    		{
    		cout<<"Please input a starting balance.\n";
    		cout<<">";
    		cin>>V.p_bal;
    		clrscr();
    		}
    	
    	else
    	if(V.p_bal != 0)		
    			{
    			cout<<"Your previous balance was "<<V.p_bal<<"\n";
    			cout<<"\nPress Enter to continue...\n";
    			cin.get();
    			clrscr();
    			}
    			char exit = 'n';
    			while(exit != 'y')
    				{
    				clrscr();
    				MENU();
    				switch(V.sel)
    				{
    				case 0 :
    				exit = 'y';
    				V.p_bal = V.total;
    				bal_o<<V.p_bal;
    				bal_o.close();
    				break;
    				
    				case 1 :
    				cout<<"Insert a value to be added to "<<V.p_bal<<"\n";
    				cout<<">";
    				cin>>V.add;
    				clrscr();
    				V.total = V.p_bal + V.add;
    				cout<<"Your new total is "<<V.total<<"\n";
    				cout<<"\nPress any key to continue...";
    				cin.get();
    				cin.get();
    				break;
    				
    				case 2 :
    				cout<<"Insert a value to be subtracted from "<<V.p_bal<<"\n";
    				cout<<">";
    				cin>>V.sub;
    				clrscr();
    				V.total = V.p_bal - V.sub;
    				cout<<"Your new total is "<<V.total<<"\n";
    				cout<<"\nPress any key to continue...";
    				cin.get();
    				cin.get();
    				break;
    				}
    			V.p_bal = V.total;		
    }
    }
    }
    				
    void MENU()
    	{
    	cout<<"\t..[]-MENU-[]..\n";
    	cout<<"\n(1) Add to "<<V.p_bal<<".\n";
    	cout<<"(2) Subtract from "<<V.p_bal<<".\n";
    	cout<<"(0) Exit\n";
    	cin>>V.sel;
    	}
    void clrscr()
    	{
    	COORD coordScreen = { 0, 0 };
        DWORD cCharsWritten;
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        DWORD dwConSize;
        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    	FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
        SetConsoleCursorPosition(hConsole, coordScreen);
        }
    Does this right to u?

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    This is the header file....
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    void clrscr();
    void MENU();
    class variables
    	{
    	public :
    	
    		float p_bal;
    		float add;
    		float sub;
    		int sel;
    		float total;
    	private :
    		
    	};
    class variables V;

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    this is balance.cpp where the value is stored

    Code:
    V.p_bal
    And im just postin all of this because there might be more people that had my prob.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing values in strings
    By scotty4598 in forum C Programming
    Replies: 9
    Last Post: 04-29-2009, 10:15 PM
  2. Storing int and double type values under one variable name
    By Thanuja91 in forum C++ Programming
    Replies: 10
    Last Post: 10-30-2007, 04:15 AM
  3. Sending values to a control
    By Zyk0tiK in forum C Programming
    Replies: 6
    Last Post: 12-02-2005, 05:29 PM
  4. Checking ascii values of char input
    By yank in forum C Programming
    Replies: 2
    Last Post: 04-29-2003, 07:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21