Saving Feature

This is a discussion on Saving Feature within the C++ Programming forums, part of the General Programming Boards category; Hey guys, im new here and i just created a Roulette program today, and i was trying to figure out ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    16

    Saving Feature

    Hey guys, im new here and i just created a Roulette program today, and i was trying to figure out how to make a save feature for it..i have an idea of how to do it, but that would only work on my personal computer..

    the way i was thinking was to ask the user if they want to save, and if they do 'yes', i would send the amount of money they have to an output file, and the output would just have 1 number in it (their money), and when they want to load, the program would read that number and then insert it into the program, and they can continue from to play with the amount of money they loaded

    the only problem is that i would send the output file to a directory on my computer, and if other people played it on their computer, they wouldnt have the same directory as me, so is there any way for the program to create a folder in the users documents called "Roulette" when they open the program ,and have all of the output files go into that folder? thanks for your help, Dan

    code for my program is right here incase you wanted to see it:
    Code:
    #include <iostream>
    #include <iomanip>
    #include <ctime>
    #include <stdlib.h>
    #include <conio.h>
    using namespace std;
    
    int PickNum();
    int SpinWheel(int choice);
    int StraightBet();
    int RedBet();
    int BlackBet();
    int EvenBet();
    int OddBet();
    int First12();
    int Second12();
    int Third12();
    
    //gobal variables
    int bank=100;
    int amountBet;
    const SIZE=100;
    //end global variables
    
    int main ()
    {  
    	srand(unsigned(time(0)));
    	  do
    	  {
    	  int choice=PickNum();
    	  SpinWheel(choice);
    	  }
    	  while(bank!=0);
    	  system("cls");
    	  if(bank==0)
    		  cout<<"\n\n\n\t\tYou lose, you don't have any more money!\n\n\n\n\n";
    	  return 0;
    }
    
    int PickNum()
    {
    	system("cls");
    	int choice;
    	cout<<"\n\n\t\tWhat do you want to bet on?\n";
    	cout<<"\n\tStraight bet(#00-36)...1"<<endl;
    	cout<<"\tRed....................2"<<endl;
    	cout<<"\tBlack..................3"<<endl;
    	cout<<"\tEven...................4"<<endl;
    	cout<<"\tOdd....................5"<<"\t\tBank: $"<<bank<<"\n";
    	cout<<"\tFirst 12...............6"<<endl;
    	cout<<"\tSecond 12..............7"<<endl;
    	cout<<"\tThird 12...............8"<<endl;
    	cout<<"\n\tChoose an option: ";
    	cin>>choice;
    	cout<<"\n\n    Amount to bet from bank: ";
    	cin>>amountBet;
    	if(amountBet>bank)
    	{
    		cout<<"\n\tYou bet more than the bank! Press any key and try again.";
    		getch();
    		PickNum();
    	}
    	return choice;
    }
    
    int SpinWheel(int choice)
    {
    	if(choice==1)
    		StraightBet();
    	if(choice==2)
    		RedBet();
    	if(choice==3)
    		BlackBet();
    	if(choice==4)
    		EvenBet();
    	if(choice==5)
    		OddBet();
    	if(choice==6)
    		First12();
    	if(choice==7)
    		Second12();
    	if(choice==8)
    		Third12();
    	return 0;
    }
    
    int StraightBet()
    {
    	system("cls");
    	int WinningNum=rand()%38+1;
    	//cout<<WinningNum;
    	int StraightBetChoice;
    	cout<<"\n\n\tChoose a number from 0-36, or 00: ";
    	cin>>StraightBetChoice;
    	if(StraightBetChoice==WinningNum)
    	{
    		cout<<"Winner!";
    		bank=bank+amountBet*25;
    		cout<<"\tYou now have $"<<bank;
    		getch();
    	}
    	if(StraightBetChoice!=WinningNum)
    	{
    		cout<<"\n\tLoser! The winning number was "<<WinningNum<<".\n\n";
    		bank=bank-amountBet;
    		cout<<"\tYou now have $"<<bank;
    		getch();
    	}
    	return 0;
    }
    
    int RedBet()
    {
    	system("cls");
    	//1 is red and 2 is black
    	int WinningNum=rand()%2+1;
    	if(WinningNum==1)
    	{
    		cout<<"\n\n\n\n\tThe number rolled was red, you are a winner!";
    		bank=bank+(amountBet*2);
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	if(WinningNum==2)
    	{
    		cout<<"\n\n\n\n\tYou lose, the number rolled was black!";
    		bank=bank-amountBet;
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	return 0;	
    }
    
    int BlackBet()
    {
    	system("cls");
    	//1 is red and 2 is black
    	int WinningNum=rand()%2+1;
    	if(WinningNum==2)
    	{
    		cout<<"\n\n\n\n\tThe number rolled was black, you are a winner!";
    		bank=bank+(amountBet*2);
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	if(WinningNum==1)
    	{
    		cout<<"\n\n\n\n\tYou lose, the number rolled was red!";
    		bank=bank-amountBet;
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	return 0;	
    }
    
    int EvenBet()
    {
    	system("cls");
    	int WinningNum=rand()%38+1;
    	if(WinningNum%2==0)
    	{
    		cout<<"\n\n\n\n\tThe number rolled was even ("<<WinningNum<<"), you are a winner!";
    		bank=bank+(amountBet*2);
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	if(WinningNum%2!=0)
    	{
    		cout<<"\n\n\n\n\tYou lose, the number rolled was odd! ("<<WinningNum<<").";
    		bank=bank-amountBet;
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	return 0;	
    }
    
    int OddBet()
    {
    	system("cls");
    	int WinningNum=rand()%38+1;
    	if(WinningNum%2!=0)
    	{
    		cout<<"\n\n\n\n\tThe number rolled was odd ("<<WinningNum<<"), you are a winner!";
    		bank=bank+(amountBet*2);
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	if(WinningNum%2==0)
    	{
    		cout<<"\n\n\n\n\tYou lose, the number rolled was even! ("<<WinningNum<<").";
    		bank=bank-amountBet;
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	return 0;	
    }
    
    int First12()
    {
    	system("cls");
    	int WinningNum=rand()%38+1;
    	if(WinningNum<=12)
    	{
    		cout<<"\n\n\n\n\tThe number rolled was in the first 12 ("<<WinningNum<<"), you are a winner!";
    		bank=bank+(amountBet*3);
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	if(WinningNum>12)
    	{
    		cout<<"\n\n\n\n\tThe number rolled was NOT in the first 12 ("<<WinningNum<<"), you lose!";
    		bank=bank-amountBet;
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	return 0;
    }
    
    int Second12()
    {
    	system("cls");
    	int WinningNum=rand()%38+1;
    	if(WinningNum>=13&&WinningNum<=24)
    	{
    		cout<<"\n\n\n\n\tThe number rolled was in the second 12 ("<<WinningNum<<"), you are a winner!";
    		bank=bank+(amountBet*3);
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	if(WinningNum<13||WinningNum>24)
    	{
    		cout<<"\n\n\n\n\tThe number rolled was NOT in the second 12 ("<<WinningNum<<"), you lose!";
    		bank=bank-amountBet;
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	return 0;
    }
    
    int Third12()
    {
    	system("cls");
    	int WinningNum=rand()%38+1;
    	if(WinningNum>24)
    	{
    		cout<<"\n\n\n\n\tThe number rolled was in the third 12 ("<<WinningNum<<"), you are a winner!";
    		bank=bank+(amountBet*3);
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	if(WinningNum<=24)
    	{
    		cout<<"\n\n\n\n\tThe number rolled was NOT in the third 12 ("<<WinningNum<<"), you lose!";
    		bank=bank-amountBet;
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    the only problem is that i would send the output file to a directory on my computer, and if other people played it on their computer, they wouldnt have the same directory as me, so is there any way for the program to create a folder in the users documents
    In C++, if you try to open a file for writing, and it doesn't exist, then the file will be created. If you don't specify a directory, then the new file will be created in the same directory as the .exe file.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by 7stud
    If you don't specify a directory, then the new file will be created in the same directory as the .exe file.
    It's create in the current directory which can be different from where the program file is. For example you open a command prompt and type the full path and name of a program but don't cd to the directory.

  4. #4
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96
    So, the program will create the folder if it does not exist just like it will create the file if it does not exist?

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    16
    wow thanks guys, i cant beleive i didnt think of that, i got the save/load feature to work, thanks again!

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    It won't create a folder for you just a file.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    So, the program will create the folder if it does not exist
    Try it out and let us know.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Google Reader feature request help
    By Wraithan in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 05-20-2009, 05:12 PM
  2. Edit and continue feature
    By George2 in forum C# Programming
    Replies: 2
    Last Post: 04-27-2008, 06:15 AM
  3. Best IDE feature
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 01-18-2004, 09:43 PM
  4. Beta-testing: new hexeditor feature
    By Sang-drax in forum C++ Programming
    Replies: 0
    Last Post: 01-05-2003, 05:30 PM
  5. File saving problem in DevC++ pls help ???
    By intruder in forum C Programming
    Replies: 3
    Last Post: 12-17-2002, 12:17 AM

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