CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Threaded View

  1. #1
    Join Date
    Mar 2011
    Posts
    52

    just looking for c++ advice

    I have only been programing for about 6 months and am just looking for some advice from a more professional programmer(that wont take much) as far as what are the main things to study.
    Anyway it would be hard to know my level(I dont even know)without any code so take this as a Hello to codeguru.com.

    This program was for my wife to keep track of what bills we have every month and she needed to be able to mark them "paid" and also be able to add and subtract them.She wanted the add/subtract to happen to all months at one time.Of course the paid would happen month by month.I also added a view to see the bill list alone.
    By the way she loves the program

    oh,there is a bug I am having a problem fixing.If you enter a number to add a element to the bill list and it is higher than the vector it crashs.I have tried all the ways i know to fix it and it wont stop the error.guess i need to brush up on throw and catch that is all i havent tried.
    Code:
    #include <stdio.h>
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <string>
    #include <cstdlib>
    
    using namespace std;
    
    int add();
    void menu();
    int getbills();
    int selection();
    
    void sleep(unsigned int mseconds)
    {
        clock_t goal = mseconds + clock();
        while (goal > clock());
    };
    
    int jan()
    {
        system("CLS");
        
             
               vector <string> janbill;///------>declurations
              int vec=0;              ///
               int y_n;              ///
               string line;         ///
                int answer=0;      ///
                int paid;         ///
                 fstream jan;    ///
                 int lp=0;//--->to be used for loop
                 jan.open("C:/bills/jan.txt");
              
          if(jan.good())//open text file and store in vector
            { while(!jan.eof())
              { getline(jan,line);
                 janbill.push_back(line);
                  cout<<vec<<"."<<line<<endl;
                   vec++;
              }
             jan.close();
            }
            else
             cout<<"file not open\n";
          
          
          cout<<"Would you like to mark a file as PAID?-----> 1.yes 2.no\n";
           cin>>answer;
            if(answer == 1)
             {
                 cout<<"*******Please enter the number of the file*********\n";
                 do{
                   cout<<"please enter a valid number\n";
                     cin>>paid;
                      }
                 while(lp > janbill.size());
                 };
                janbill[paid]=janbill[paid]+" <----PAID";
                 ofstream janout("C:/bills/jan.txt");
                  if(janout.good())
                   {for(int i=0;i!=janbill.size();i++)
                     {
                            janout << janbill[i];
                            if(i != janbill.size()-1)
                            janout<<endl;
                            sleep(100);
                     };
                          janout.flush();
                          janout.close();
                   }
                 else if(answer==2) 
                  {getbills();}
                   else 
                    cout<<"please enter a valid selection\n";
                     getbills();
     }//------>ect ect copy and pasted same code for all months with exception of the file name
    
    void menu()
    {
         system("CLS");
          cout<<"              *********MAIN MENU********** \n";
          cout<<"              *        1.add bill        *     \n";
          cout<<"              *        2.view bills      *     \n";
          cout<<"              *        3.delete bill     *     \n";
          cout<<"              *        4.view bill list  *     \n";
          cout<<"              *        5.exit            *     \n";
          cout<<"              ****************************\n";
          selection();
          };
          
    int add()
    {
        system("CLS");
         fstream in;
          int vec=0;
           string line;
        
        cout<<"********** CURRENT LIST ************\n";
        
        in.open("C:/bills/default.txt");
         if(in.good())//open text file and store in vector
            { while(!in.eof())
              { getline(in,line);
                 cout<<vec<<"."<<line<<endl;
                  vec++;
                  }
              }
            in.close();
            
        cout<<"Please enter the name of the bill to be added.\n"
            <<"       ----> to EXIT enter X <----\n";
         string newbill;
          cin>>newbill;
           if(newbill == "x" || newbill == "X")
             menu();
            //add this name to the end of all months
           ofstream add;
        
        
          add.open("C:/bills/dec.txt",ofstream::app);
          if(add.good())
           {add<<endl<<newbill<<flush;
            add.close();}
         
           add.open("C:/bills/jan.txt",ofstream::app);
            if(add.good())
            {add<<endl<<newbill<<flush;
             add.close();}
    ect ect.....
      menu();     
                 
    };
    
    int getbills()
    {
      int month;
       system("CLS");
        cout<<"*********please select a month***********\n";
        cout<<"       1.january\n"
            <<"       2.febuary\n"
            <<"       3.march\n"
            <<"       4.april\n"
            <<"       5.may\n"
            <<"       6.june\n"
            <<"       7.july\n"
            <<"       8.august\n"
            <<"       9.september\n"
            <<"      10.october\n"
            <<"      11.november\n"
            <<"      12.december\n"
            <<"      13.**** LAST PAGE ****\n"
            <<"      14.**** CLOSE ALL ****\n";
            cin>>month;
        
       switch(month){
         case 1:jan();
          case 2:feb();
           case 3:march();
            case 4:april();
             case 5:may();
              case 6:june();
               case 7:july();
                case 8:aug();
                 case 9:sept();
                  case 10:oct();
                   case 11:nov();
                    case 12:dec();
                     case 13:menu();
                      case 14:exit(0);
         default:cout<<"please enter a number 1-14",sleep(3000),getbills();//clears and restarts this function
    };
        
        
        
    }
    
    void del()
    {
        
     system("CLS");
        cout<<"********** CURRENT LIST ************\n";
         ///////////////////////////////////////////////////
        vector <string> dlist;///------>declurations
         int vec=0;              ////
          int y_n=0;            /////  
           string line;        //////    
            int answer=0;     ///////
             int paid=0;     ////////
             
      ifstream jan;      
       jan.open("C:/bills/default.txt");
              
          if(jan.is_open())/////////////////////////shows a list so the user can pick one to delete
            { while(!jan.eof())//////////////////
              { getline(jan,line);//////////////
                 dlist.push_back(line);////////
                 cout<<vec<<"."<<line<<endl;//
                  vec++;/////////////////////
              }
             }
            jan.close();
       ///////////////////delete menu//////////////////////////////////////    
       cout<<"   please enter the number for the bill you want to delete\n";
       cout<<"    ----------------> press 00 to exit<------------\n";
       cin>>answer;
       if(answer == 00)
        menu();
         dlist.erase(dlist.begin()+answer);//<--delete's the file from the vector
         
         /////////////////print new file to all files//////////////////////
          ofstream janout;
          janout.open("C:/bills/default.txt");
                  if(janout.good())
                   {for(int i=0 ;i != dlist.size() ; i++)
                     {
                            janout << dlist[i];
                            if(i != dlist.size()-1)
                            {janout<<endl;}
                            
                     };
                   }
                   else
                    {cout<<"file didnt open\n";};
                  janout.close();
    ect ect.....
    Last edited by josh26757; March 1st, 2011 at 08:07 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured