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

Thread: just looking for c++ advice

  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.

  2. #2
    Join Date
    Mar 2011
    Posts
    52

    Re: just looking for c++ advice

    didnt have enough lines lol
    i should add that i am trying to get ahead before i go into college.i would estimate fall.(work paperwork has to go through)
    the real question is
    how am i doing for 6 months and what are going to be the best things to make sure i know before school?
    Last edited by josh26757; March 1st, 2011 at 10:42 PM.

  3. #3
    Join Date
    Jun 2009
    Location
    oklahoma
    Posts
    199

    Re: just looking for c++ advice

    Well your code is impossible to read with all the insane formatting going on.
    I see that you have a large switch statement for month input.
    You need break statements after each case, otherwise when a user enters '1' for January, it will also run feb() march() april() and so on.

    If you are planning on completing a 4 year Computer Science program, then you won't need much programming experience. However, you won't even be learning how to program per say... tons of people graduate with a CS degree but do not know how to program.

  4. #4
    Join Date
    Apr 2008
    Posts
    726

    Re: just looking for c++ advice

    Quote Originally Posted by jnmacd View Post
    Well your code is impossible to read with all the insane formatting going on.
    I see that you have a large switch statement for month input.
    You need break statements after each case, otherwise when a user enters '1' for January, it will also run feb() march() april() and so on.

    If you are planning on completing a 4 year Computer Science program, then you won't need much programming experience. However, you won't even be learning how to program per say... tons of people graduate with a CS degree but do not know how to program.
    I would say that is not possible for CS courses in the UK

  5. #5
    Join Date
    Apr 2008
    Posts
    726

    Re: just looking for c++ advice

    to OP, mark in your code where it crashes. It sounds as though it is somewhere where you use operator[] on a vector, but there are lots of those in your code....

    if its this:
    Code:
    cin >> paid;
    ...
    janbill[paid]=janbill[paid]+" <----PAID";
    you need to check paid value first:

    Code:
    cin >> paid;
    ...
    if (paid < janbill.size())
      janbill[paid]=janbill[paid]+" <----PAID";
    else
      cout << paid << " is not a valid entry" << endl;

    Can I also say that copy/pasting large chunks of code, like you have done for jan.feb/mar etc is often an indicator that you can clean up your code with functions.

    e.g.
    Code:
    int jan();
    int feb();
    ... etc
    can be replaced with ONE function
    Code:
    int month(string const & filename);
    Last edited by Amleto; March 2nd, 2011 at 04:42 AM.

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: just looking for c++ advice

    Quote Originally Posted by josh26757 View Post
    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.
    Have a look at the recommended books in the FAQ section. Lots of them deal with medium to advanced topics. Looking at your code, I think you could learn a lot from the book "C++ coding standards". Also check out http://www.parashift.com/c++-faq-lite/.

    As to your bug: when you learn to program it's important to learn to debug your programs too. This isn't always thought in school, but you'll likely be able to find some online tutorials for your compiler/environment. The rest is just practice.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  7. #7
    Join Date
    Mar 2011
    Posts
    52

    Re: just looking for c++ advice

    thanks all!! I knew i could have cleaned up the code alot and use some kind of recursive call the problem i ran into is that i wanted to put each month in a different file in this case a text file.

    i will be sure to check out the books.
    Amleto you may be correct.I cant remember the dif. ways i tried it .
    I know I tried a
    Code:
    if(paid>janbill.size())
     {cout<<"please enter a valid selection"
       while(paid>janbill.size())
         cin>>paid;
     }
    I know that looks really sloppy and didnt like so I left it out.
    I will def. spend some time breaking down the functions.
    thanks for all the comments so far just looking at the forums you guys are very impressive.

  8. #8
    Join Date
    Mar 2011
    Posts
    52

    Re: just looking for c++ advice

    thanks for such good advice.i had to work 12 hours today so sorry i didnt have time to get back on here and comment.
    thanks for the advice on the case statement i completely forgot about the break.

    the program crashs just where you guessed.if you try to add "paid" to a number to a vector element that isnt there.my problem is that i tried to catch it and it seems to skip over my loop.
    now that i think about it,it may have something to do with the case-break problem.

    anyway my goal is to break down the program and to consolidate the code the best i can.after that i will repost in this thread and see if you all have anymore advice on the way i am coding
    the program.i do agree it is hard to read and i can do much better.let me try to clean it up and go from there.

    once again thank you all.by looking through the posts here.i am very glad i found this site.you all rock so thanks a lot for your time.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)