Page 2 of 2 FirstFirst 12
Results 26 to 38 of 38

Thread: My C++ programs

  1. #26
    Join Date
    Jan 2012
    Posts
    915
    Mentioned
    13 Post(s)
    Quoted
    87 Post(s)

    Default

    OH! Sweet. I did something right when learning. I'm using CodeBlocks, too!

    Kewl. Anyway, Brandon, Daniel, Could you take a look at my new code? I made some changes, and added perimeter.

  2. #27
    Join Date
    Dec 2011
    Location
    -bash
    Posts
    515
    Mentioned
    0 Post(s)
    Quoted
    27 Post(s)

    Default

    You didn't put a catch for when the user enters a value that is not 0, 1, or 2. So if I put 3, it just exits...

    Fix: a simple label is enough to fix this.

    main()
    start:
    if (...............)
    else if (...............)
    else if (...............)
    else
    goto start;
    end;

  3. #28
    Join Date
    Jan 2012
    Posts
    915
    Mentioned
    13 Post(s)
    Quoted
    87 Post(s)

    Default

    I don't fully understand. Could you go into more detail?

    Thanks for helping!

  4. #29
    Join Date
    Dec 2011
    Location
    -bash
    Posts
    515
    Mentioned
    0 Post(s)
    Quoted
    27 Post(s)

    Default

    Labels are just overs simplified loops and are very useful if programming in very low level languages like any assembly language (MIPS). However that doesn't mean we can't make use of them in c++.

    Like this:

    C++ Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;

    void Welcome()
    {
      cout << "\nWelcome to Vinyl Scratch's MathFinder!\n";
      cout << "It's my first C++ program, so I wanted to \nsee "<<
      "if you guys liked it, and if I did well.\n\n\n";
    }


    int main()
    {
     
      unsigned long Width, Height, Base, Width2, Length2;
      Welcome();
     
      enum mathType {Area, Volume, Perimeter};
     
      mathType Math;
      int x;
     
      start://label
     
      cout << "What math type would you like?(0-2) Area is 0, Volume is 1, and Perimeter is 2.\r\n";
      cin >> x;
      Math = mathType(x);
     
      if (Math == Area)
      {
        if (!cin.good())
        {
          cout << "Invalid Character, please input an integer.\r\n";
          return 1;
        }
       
        cout << "Enter Width then Length:" << "\r\n";
        cin >> Width;
        cin >> Height;
        cout << "Area is: " << Width * Height << endl;
      }
     
     
      else if (Math == Volume)
      {
        if (!cin.good())
        {
          cout << "Invalid Character, please input an integer.\r\n";
          return 1;
        }
       
        cout << "Enter Width, then Height, then Base:\r\n";
        cin >> Width;
        cin >> Height;
        cin >> Base;
        cout << "Volume is: " << Width * Height * Base << endl;
      }
     
      else if (Math == Perimeter)
      {
        if (!cin.good())
        {
          cout << "Invalid Character, please input an integer.\r\n";
          return 1;
        }
       
        cout << "Enter Width, then Length.\r\n";
        cin >> Width;
        cin >> Height;
       
        Width2 = Width * 2;
        Length2 = Height * 2;
       
        cout << "Perimeter is: " << Width2 + Length2 << endl;
      }
     
      else
        goto start; //go to label
     
      return 0;
    }


    OR THIS:


    C++ Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;

    enum mathType {Area, Volume, Perimeter};

    void Welcome();//
    void Operation(mathType );//function headers

    int main()
    {
      Welcome();
      mathType Math;
     
      int x;
      DoSomeMath://label
     
      cout << "What math type would you like?(0-2) \n"<<
      "Area is 0, Volume is 1, and Perimeter is 2.\n";
      cin >> x;
     
      if (x < 0 || x > 2)
        goto DoSomeMath;
     
      Math = mathType(x);
      Operation(Math);
     
      return 0;
    }

    void Welcome()
    {
      cout << "\nWelcome to Vinyl Scratch's MathFinder!\n";
      cout << "It's my first C++ program, so I wanted to \nsee "<<
      "if you guys liked it, and if I did well.\n\n\n";
    }

    void Operation(mathType MATH)
    {
      unsigned long Width, Height, Base, Width2, Length2;
     
      if (MATH == Area)
      {
        if (!cin.good())
        {
          cout << "Invalid Character, please input an integer.\n";
          return;
        }
       
        cout << "Enter Width then Length:" << "\n";
        cin >> Width;
        cin >> Height;
        cout << "Area is: " << Width * Height << endl;
      }
      else if (MATH == Volume)
      {
        if (!cin.good())
        {
          cout << "Invalid Character, please input an integer.\r\n";
          return;
        }
       
        cout << "Enter Width, then Height, then Base:\r\n";
        cin >> Width;
        cin >> Height;
        cin >> Base;
        cout << "Volume is: " << Width * Height * Base << endl;
      }
      else if (MATH == Perimeter)
      {
        if (!cin.good())
        {
          cout << "Invalid Character, please input an integer.\r\n";
          return;
        }
       
        cout << "Enter Width, then Length.\r\n";
        cin >> Width;
        cin >> Height;
       
        Width2 = Width * 2;
        Length2 = Height * 2;
       
        cout << "Perimeter is: " << Width2 + Length2 << endl;
      }
    }


    Someone did message me though and said that using labels isn't such a swell idea and I see why now. Labels just aren't as solid as using for/while loops. So if you want, you can go ahead and implements some while loops.

    gl


    while loop:

    C++ Code:
    #include <iostream>
    #include <sstream>
    using std::cout;
    using std::cin;
    using std::endl;
    using std::string;
    using std::stringstream;

    enum mathType {Area, Volume, Perimeter};

    mathType Getx ();
    void Welcome();
    void Operation(mathType );

    int main()
    {
      Welcome();

      Operation(Getx());
     
      return 0;
    }

    mathType Getx()
    {
      int x;
      string input = "";
     
      while (1)
      {
        cout << "What math type would you like?(0-2) \n"<<
        "Area is 0, Volume is 1, and Perimeter is 2.\n";
        getline(cin, input);
       
        stringstream CatchBad(input);
       
        if ((CatchBad >> x))
        {
          if (x < 0 || x > 2)
        continue;
          else
        break;
        }
        else
          cout << "\nInvalid Character, please input an integer.\n";
      }
      return mathType(x);
    }

    void Welcome()
    {
      cout << "\nWelcome to Vinyl Scratch's MathFinder!\n";
      cout << "It's my first C++ program, so I wanted to \nsee "<<
      "if you guys liked it, and if I did well.\n\n\n";
    }

    void Operation(mathType MATH)
    {
      unsigned long Width, Height, Base, Width2, Length2;
     
      if (MATH == Area)
      {    
        cout << "Enter Width then Length:" << "\n";
        cin >> Width;
        cin >> Height;
        cout << "Area is: " << Width * Height << endl;
      }
     
      else if (MATH == Volume)
      {
        cout << "Enter Width, then Height, then Base:\n";
        cin >> Width;
        cin >> Height;
        cin >> Base;
        cout << "Volume is: " << Width * Height * Base << endl;
      }
     
      else if (MATH == Perimeter)
      {
        cout << "Enter Width, then Length.\n";
        cin >> Width;
        cin >> Height;
       
        Width2 = Width * 2;
        Length2 = Height * 2;
       
        cout << "Perimeter is: " << Width2 + Length2 << endl;
      }
    }
    Last edited by Recursive; 01-17-2013 at 06:58 AM. Reason: What are labels?

  5. #30
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    I left you that +1 Reputation for the idea but suggested not to use Goto because if used incorrectly, it "skips".. By that I mean, if a variable X already contains input and a user uses a goto statement before that X, rather than asking for input again in a loop, the goto will not even bother asking for input.. Instead, it skips the asking and keeps the previously stored value..

    Also, if a bad-bit/flag is set (especially on a stream), it will NOT initialize a new stream or clear the bit.. It will continue on without any errors or letting the user know there is a problem.. The state of the stream stays the same!

    Streams like cin and cout will stay the same.. cin will not clear, it will contain the same input it had before and rather than ask for new input, it will use what it already has.. Goto in ASM is a bit different.

    Goto can be used in C/C++ but use it carefully and wisely or else you'll end up with trouble. There is almost no situations other than insane nested loops where goto would be required to break out/jump abruptly.

    Dijkstra also gave a reason why it's bad.. You can google it for more information on why it should be used with care/rarely.
    Last edited by Brandon; 01-17-2013 at 11:27 PM.
    I am Ggzz..
    Hackintosher

  6. #31
    Join Date
    Jan 2011
    Location
    Denver, CO
    Posts
    1,351
    Mentioned
    2 Post(s)
    Quoted
    72 Post(s)

    Default

    Quote Originally Posted by [Re]cUrSivE View Post
    C++ Code:
    if (MATH == Area)
      {    
        cout << "Enter Width then Length:" << "\n";
        cin >> Width;
        cin >> Height;
        cout << "Area is: " << Width * Height << endl;
      }
     
      else if (MATH == Volume)
      {
        cout << "Enter Width, then Height, then Base:\n";
        cin >> Width;
        cin >> Height;
        cin >> Base;
        cout << "Volume is: " << Width * Height * Base << endl;
      }
     
      else if (MATH == Perimeter)
      {
        cout << "Enter Width, then Length.\n";
        cin >> Width;
        cin >> Height;
       
        Width2 = Width * 2;
        Length2 = Height * 2;
       
        cout << "Perimeter is: " << Width2 + Length2 << endl;
      }
    }
    You can use switch statements with enumerations since they are integer constants, it makes this snippet cleaner too:

    C++ Code:
    switch (MATH) {
    case Area:
        cout << "Enter Width then Length:" << "\n";
        cin >> Width;
        cin >> Height;
        cout << "Area is: " << Width * Height << endl;
        break;
       
    case Volume:
        cout << "Enter Width, then Height, then Base:\n";
        cin >> Width;
        cin >> Height;
        cin >> Base;
        cout << "Volume is: " << Width * Height * Base << endl;
        break;
       
    case Perimeter:
        cout << "Enter Width, then Length.\n";
        cin >> Width;
        cin >> Height;
       
        Width2 = Width * 2;
        Length2 = Height * 2;
       
        cout << "Perimeter is: " << Width2 + Length2 << endl;
    }

  7. #32
    Join Date
    Jan 2012
    Posts
    915
    Mentioned
    13 Post(s)
    Quoted
    87 Post(s)

    Default

    Thanks, guys! I know alot more, and when I need to refer back to here, I'll have the information here so that I can understand. You guys are great!

  8. #33
    Join Date
    Dec 2011
    Location
    -bash
    Posts
    515
    Mentioned
    0 Post(s)
    Quoted
    27 Post(s)

    Default

    Btw Vinyl Scratch, if you are interested in the math aspect of programming, join project euler. Project euler is very helpful and I'd say I learned a lot of programming by solving problems there. I think my sig contains a link or you can just type in http://projecteuler.net/problems. My friend code is 26216094381320_b3d58f83f92949f0e5e0f71e3fc6122d. Add me if you join

    Quote Originally Posted by Echo_ View Post
    You can use switch statements with enumerations since they are integer constants, it makes this snippet cleaner too:
    You know I tried doing that, but I got some error. So I just decided to stick to what I was doing

  9. #34
    Join Date
    Nov 2011
    Location
    MA
    Posts
    545
    Mentioned
    3 Post(s)
    Quoted
    10 Post(s)

    Default

    Quote Originally Posted by [Re]cUrSivE View Post
    Btw Vinyl, if you are interested in the math aspect of programming, join project euler. Project euler is very helpful and I'd say I learned a lot of programming by solving problems there. I think my sig contains a link or you can just type in http://projecteuler.net/problems. My friend code is 26216094381320_b3d58f83f92949f0e5e0f71e3fc6122d. Add me if you join
    I'd also recommend Project Euler for learning any programming language. Question 1 with Simba

    Simba Code:
    program One;
    {
     If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
     Find the sum of all the multiples of 3 or 5 below 1000.
    }


    var
      i, totalsum: integer;
      Fin, Fin2: extended;
      ArrFin, ArrFin2, SumArr, counted: array of integer;

    Function NoRemain(FirstNum, SecondNum: extended): boolean;
    begin
      Fin := (FirstNum / SecondNum);
      Fin2 := Round(Fin);
      if (Fin = Fin2) then
        Result := true;
    end;

    begin
      SetLength(ArrFin, 1050);
      SetLength(Arrfin2, 1050);
      SetLength(Counted, 2100);
      for i := 0 to 999 do
      begin
        if (NoRemain(i, 5) = true) then
          ArrFin[i] := round((Fin) * 5);
      end;
      for i := 0 to 999 do
      begin
        if (NoRemain(i, 3) = true) then
          ArrFin2[i] := round((Fin) * 3);
      end;
      SumArr := CombineIntArray(ArrFin, Arrfin2);
      for i := 0 to (Length(SumArr) - 1) do
        if InIntArray(Counted, SumArr[i]) then
          Totalsum := TotalSum
        else
        begin
          TotalSum := Totalsum + SumArr[i];
          Counted[i] := SumArr[i];
        end;
      ClearDebug;
      WriteLn('Sum: ' + IntToStr(TotalSum));
    end.

  10. #35
    Join Date
    Jan 2011
    Location
    Denver, CO
    Posts
    1,351
    Mentioned
    2 Post(s)
    Quoted
    72 Post(s)

    Default

    Quote Originally Posted by tehq View Post
    I'd also recommend Project Euler for learning any programming language. Question 1 with Simba

    Simba Code:
    program One;
    {
     If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
     Find the sum of all the multiples of 3 or 5 below 1000.
    }


    var
      i, totalsum: integer;
      Fin, Fin2: extended;
      ArrFin, ArrFin2, SumArr, counted: array of integer;

    Function NoRemain(FirstNum, SecondNum: extended): boolean;
    begin
      Fin := (FirstNum / SecondNum);
      Fin2 := Round(Fin);
      if (Fin = Fin2) then
        Result := true;
    end;

    begin
      SetLength(ArrFin, 1050);
      SetLength(Arrfin2, 1050);
      SetLength(Counted, 2100);
      for i := 0 to 999 do
      begin
        if (NoRemain(i, 5) = true) then
          ArrFin[i] := round((Fin) * 5);
      end;
      for i := 0 to 999 do
      begin
        if (NoRemain(i, 3) = true) then
          ArrFin2[i] := round((Fin) * 3);
      end;
      SumArr := CombineIntArray(ArrFin, Arrfin2);
      for i := 0 to (Length(SumArr) - 1) do
        if InIntArray(Counted, SumArr[i]) then
          Totalsum := TotalSum
        else
        begin
          TotalSum := Totalsum + SumArr[i];
          Counted[i] := SumArr[i];
        end;
      ClearDebug;
      WriteLn('Sum: ' + IntToStr(TotalSum));
    end.
    Question 1 with Clojure:
    LISP Code:
    (println "sum:" (reduce + (set (concat (range 0 1000 3) (range 0 1000 5)))))


  11. #36
    Join Date
    Dec 2011
    Location
    -bash
    Posts
    515
    Mentioned
    0 Post(s)
    Quoted
    27 Post(s)

    Default

    I solved questions 1-5 with javascript on khanacademy then for the most part I use c/c++ or bash/awk

  12. #37
    Join Date
    Jan 2012
    Posts
    915
    Mentioned
    13 Post(s)
    Quoted
    87 Post(s)

  13. #38
    Join Date
    Jan 2012
    Posts
    915
    Mentioned
    13 Post(s)
    Quoted
    87 Post(s)

    Default

    c++ Code:
    #include <iostream>

    using namespace std;

    void Math()
    {
        unsigned long Width, Height, Length, Lateral, Length2, Width2, LengthAndWidth, Area, Total, AreaTimesTwo;

        cout << "Enter the length, then width then height\r\n";
        cin >> Length;
        cin >> Width;
        cin >> Height;
        Length2 = Length * 2;
        Width2 = Width * 2;
        LengthAndWidth = Length2 + Width2;
        Lateral = LengthAndWidth * Height;
        Area = Width * Length;
        AreaTimesTwo = Area * 2;
        Total = Lateral + AreaTimesTwo;

        cout << "Your Lateral is:" << Lateral << endl;
        cout << "Your total Surface Area is:" << Total << endl;

    }

    int main()
    {
        start:

        Math();

        goto start;

        return 0;
    }

Page 2 of 2 FirstFirst 12

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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