Manipulating user input

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • agent349

    Manipulating user input

    Hi,

    I'm fairly new to c++. I need user input in the form of dollar
    amounts, ie: "$10.00". I'd like to remove the dollar sign "$" then
    store the rest in a variable. How do I go about removing the dollar
    sign? Thanks in advance!
  • Mike Wahler

    #2
    Re: Manipulating user input


    "agent349" <agent349@yahoo .com> wrote in message
    news:e23997a6.0 404082133.70087 8a0@posting.goo gle.com...[color=blue]
    > Hi,
    >
    > I'm fairly new to c++. I need user input in the form of dollar
    > amounts, ie: "$10.00". I'd like to remove the dollar sign "$" then
    > store the rest in a variable. How do I go about removing the dollar
    > sign? Thanks in advance![/color]

    std::string input;
    int amount(0);

    std::getline(st d::cin, input);
    if(!input.empty ())
    {
    std::istringstr eam iss(input.subst r(1));
    iss >> amount;
    }

    std::cout << '$' << amount / 100 << '.' << amount % 100 << '\n';

    -Mike


    Comment

    • Mike Wahler

      #3
      Re: Manipulating user input


      "agent349" <agent349@yahoo .com> wrote in message
      news:e23997a6.0 404082133.70087 8a0@posting.goo gle.com...[color=blue]
      > Hi,
      >
      > I'm fairly new to c++. I need user input in the form of dollar
      > amounts, ie: "$10.00". I'd like to remove the dollar sign "$" then
      > store the rest in a variable. How do I go about removing the dollar
      > sign? Thanks in advance![/color]

      std::string input;
      int amount(0);

      std::getline(st d::cin, input);
      if(!input.empty ())
      {
      std::istringstr eam iss(input.subst r(1));
      iss >> amount;
      }

      std::cout << '$' << amount / 100 << '.' << amount % 100 << '\n';

      -Mike


      Comment

      • John Harrison

        #4
        Re: Manipulating user input


        "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message
        news:%2rdc.2855 $A_4.2826@newsr ead1.news.pas.e arthlink.net...[color=blue]
        >
        > "agent349" <agent349@yahoo .com> wrote in message
        > news:e23997a6.0 404082133.70087 8a0@posting.goo gle.com...[color=green]
        > > Hi,
        > >
        > > I'm fairly new to c++. I need user input in the form of dollar
        > > amounts, ie: "$10.00". I'd like to remove the dollar sign "$" then
        > > store the rest in a variable. How do I go about removing the dollar
        > > sign? Thanks in advance![/color]
        >
        > std::string input;
        > int amount(0);
        >
        > std::getline(st d::cin, input);
        > if(!input.empty ())
        > {
        > std::istringstr eam iss(input.subst r(1));
        > iss >> amount;
        > }
        >
        > std::cout << '$' << amount / 100 << '.' << amount % 100 << '\n';
        >
        > -Mike
        >[/color]

        I think you are forgetting the period in the input.

        john


        Comment

        • John Harrison

          #5
          Re: Manipulating user input


          "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message
          news:%2rdc.2855 $A_4.2826@newsr ead1.news.pas.e arthlink.net...[color=blue]
          >
          > "agent349" <agent349@yahoo .com> wrote in message
          > news:e23997a6.0 404082133.70087 8a0@posting.goo gle.com...[color=green]
          > > Hi,
          > >
          > > I'm fairly new to c++. I need user input in the form of dollar
          > > amounts, ie: "$10.00". I'd like to remove the dollar sign "$" then
          > > store the rest in a variable. How do I go about removing the dollar
          > > sign? Thanks in advance![/color]
          >
          > std::string input;
          > int amount(0);
          >
          > std::getline(st d::cin, input);
          > if(!input.empty ())
          > {
          > std::istringstr eam iss(input.subst r(1));
          > iss >> amount;
          > }
          >
          > std::cout << '$' << amount / 100 << '.' << amount % 100 << '\n';
          >
          > -Mike
          >[/color]

          I think you are forgetting the period in the input.

          john


          Comment

          • John Harrison

            #6
            Re: Manipulating user input


            "agent349" <agent349@yahoo .com> wrote in message
            news:e23997a6.0 404082133.70087 8a0@posting.goo gle.com...[color=blue]
            > Hi,
            >
            > I'm fairly new to c++. I need user input in the form of dollar
            > amounts, ie: "$10.00". I'd like to remove the dollar sign "$" then
            > store the rest in a variable. How do I go about removing the dollar
            > sign? Thanks in advance![/color]

            What sort of variable? int, double, some custom money type?

            Integer variables are the best for manipulating money, just count you money
            in cents, and then convert to dollars when you have to output figures. A
            typical newbie error is 'money prints with a period, therefore I must use a
            double or a float because they print with periods'. That is an error, money
            is an integral quantity, you only ever have a whole number of cents,
            therefore integers are the correct thing to use. How you print a money total
            has got nothing to do with it.

            There are lots of ways to do what you want, here's one way. It works but it
            may not do exactly what you want since your requirements are not clearly
            defined.

            #include <iostream>
            #include <iomanip>
            using namespace std;

            int main()
            {
            cout << "enter some money (like this $12.34) "
            char dummy1, dummy2;
            int dollars, cents;
            cin >> dummy1 >> dollars >> dummy2 >> cents;
            int total_cents = 100*dollars + cents;

            cout << "You entered $" << total_cents/100 << '.' << setfill('0') <<
            setw(2) << total_cents%100 << '\n';
            }

            This is untested code.

            The dummy1 and dummy2 variables are there just to absorb the dollar sign and
            period from the input.

            The last line shows how you can pretty print a money value from an int
            variable which is a number of cents.

            The big problem with this program is that it makes no attempt to deal with
            incorrect input on the part of the user, it will accept some incorrect input
            and barf on others. But you didn't specify what you wanted to happen on
            incorrect input (or even what correct input was) so I ignored that issue.

            john


            Comment

            • John Harrison

              #7
              Re: Manipulating user input


              "agent349" <agent349@yahoo .com> wrote in message
              news:e23997a6.0 404082133.70087 8a0@posting.goo gle.com...[color=blue]
              > Hi,
              >
              > I'm fairly new to c++. I need user input in the form of dollar
              > amounts, ie: "$10.00". I'd like to remove the dollar sign "$" then
              > store the rest in a variable. How do I go about removing the dollar
              > sign? Thanks in advance![/color]

              What sort of variable? int, double, some custom money type?

              Integer variables are the best for manipulating money, just count you money
              in cents, and then convert to dollars when you have to output figures. A
              typical newbie error is 'money prints with a period, therefore I must use a
              double or a float because they print with periods'. That is an error, money
              is an integral quantity, you only ever have a whole number of cents,
              therefore integers are the correct thing to use. How you print a money total
              has got nothing to do with it.

              There are lots of ways to do what you want, here's one way. It works but it
              may not do exactly what you want since your requirements are not clearly
              defined.

              #include <iostream>
              #include <iomanip>
              using namespace std;

              int main()
              {
              cout << "enter some money (like this $12.34) "
              char dummy1, dummy2;
              int dollars, cents;
              cin >> dummy1 >> dollars >> dummy2 >> cents;
              int total_cents = 100*dollars + cents;

              cout << "You entered $" << total_cents/100 << '.' << setfill('0') <<
              setw(2) << total_cents%100 << '\n';
              }

              This is untested code.

              The dummy1 and dummy2 variables are there just to absorb the dollar sign and
              period from the input.

              The last line shows how you can pretty print a money value from an int
              variable which is a number of cents.

              The big problem with this program is that it makes no attempt to deal with
              incorrect input on the part of the user, it will accept some incorrect input
              and barf on others. But you didn't specify what you wanted to happen on
              incorrect input (or even what correct input was) so I ignored that issue.

              john


              Comment

              • David Harmon

                #8
                Re: Manipulating user input

                On Fri, 09 Apr 2004 06:05:47 GMT in comp.lang.c++, "Mike Wahler"
                <mkwahler@mkwah ler.net> wrote,[color=blue]
                >std::getline(s td::cin, input);
                >if(!input.empt y())
                >{
                > std::istringstr eam iss(input.subst r(1));
                > iss >> amount;
                >}[/color]

                Perhaps excessive?
                if (cin.peek() == '$')
                cin.ignore(1);

                Comment

                • David Harmon

                  #9
                  Re: Manipulating user input

                  On Fri, 09 Apr 2004 06:05:47 GMT in comp.lang.c++, "Mike Wahler"
                  <mkwahler@mkwah ler.net> wrote,[color=blue]
                  >std::getline(s td::cin, input);
                  >if(!input.empt y())
                  >{
                  > std::istringstr eam iss(input.subst r(1));
                  > iss >> amount;
                  >}[/color]

                  Perhaps excessive?
                  if (cin.peek() == '$')
                  cin.ignore(1);

                  Comment

                  • Mike Wahler

                    #10
                    Re: Manipulating user input


                    "John Harrison" <john_andronicu s@hotmail.com> wrote in message
                    news:c55j5c$2mt 6ki$1@ID-196037.news.uni-berlin.de...[color=blue]
                    >
                    > "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message
                    > news:%2rdc.2855 $A_4.2826@newsr ead1.news.pas.e arthlink.net...[color=green]
                    > >
                    > > "agent349" <agent349@yahoo .com> wrote in message
                    > > news:e23997a6.0 404082133.70087 8a0@posting.goo gle.com...[color=darkred]
                    > > > Hi,
                    > > >
                    > > > I'm fairly new to c++. I need user input in the form of dollar
                    > > > amounts, ie: "$10.00". I'd like to remove the dollar sign "$" then
                    > > > store the rest in a variable. How do I go about removing the dollar
                    > > > sign? Thanks in advance![/color]
                    > >
                    > > std::string input;
                    > > int amount(0);
                    > >
                    > > std::getline(st d::cin, input);
                    > > if(!input.empty ())
                    > > {
                    > > std::istringstr eam iss(input.subst r(1));
                    > > iss >> amount;
                    > > }
                    > >
                    > > std::cout << '$' << amount / 100 << '.' << amount % 100 << '\n';
                    > >
                    > > -Mike
                    > >[/color]
                    >
                    > I think you are forgetting the period in the input.[/color]

                    Had to leave something for OP to do. :-)

                    -Mike


                    Comment

                    • Mike Wahler

                      #11
                      Re: Manipulating user input


                      "John Harrison" <john_andronicu s@hotmail.com> wrote in message
                      news:c55j5c$2mt 6ki$1@ID-196037.news.uni-berlin.de...[color=blue]
                      >
                      > "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message
                      > news:%2rdc.2855 $A_4.2826@newsr ead1.news.pas.e arthlink.net...[color=green]
                      > >
                      > > "agent349" <agent349@yahoo .com> wrote in message
                      > > news:e23997a6.0 404082133.70087 8a0@posting.goo gle.com...[color=darkred]
                      > > > Hi,
                      > > >
                      > > > I'm fairly new to c++. I need user input in the form of dollar
                      > > > amounts, ie: "$10.00". I'd like to remove the dollar sign "$" then
                      > > > store the rest in a variable. How do I go about removing the dollar
                      > > > sign? Thanks in advance![/color]
                      > >
                      > > std::string input;
                      > > int amount(0);
                      > >
                      > > std::getline(st d::cin, input);
                      > > if(!input.empty ())
                      > > {
                      > > std::istringstr eam iss(input.subst r(1));
                      > > iss >> amount;
                      > > }
                      > >
                      > > std::cout << '$' << amount / 100 << '.' << amount % 100 << '\n';
                      > >
                      > > -Mike
                      > >[/color]
                      >
                      > I think you are forgetting the period in the input.[/color]

                      Had to leave something for OP to do. :-)

                      -Mike


                      Comment

                      • Kevin Goodsell

                        #12
                        Re: Manipulating user input

                        David Harmon wrote:
                        [color=blue]
                        > On Fri, 09 Apr 2004 06:05:47 GMT in comp.lang.c++, "Mike Wahler"
                        > <mkwahler@mkwah ler.net> wrote,
                        >[color=green]
                        >>std::getline( std::cin, input);
                        >>if(!input.emp ty())
                        >>{
                        >> std::istringstr eam iss(input.subst r(1));
                        >> iss >> amount;
                        >>}[/color]
                        >
                        >
                        > Perhaps excessive?
                        > if (cin.peek() == '$')
                        > cin.ignore(1);
                        >[/color]

                        I think there's a 0-argument ignore() for removing 1 character.

                        -Kevin
                        --
                        My email address is valid, but changes periodically.
                        To contact me please use the address from a recent posting.

                        Comment

                        • Kevin Goodsell

                          #13
                          Re: Manipulating user input

                          David Harmon wrote:
                          [color=blue]
                          > On Fri, 09 Apr 2004 06:05:47 GMT in comp.lang.c++, "Mike Wahler"
                          > <mkwahler@mkwah ler.net> wrote,
                          >[color=green]
                          >>std::getline( std::cin, input);
                          >>if(!input.emp ty())
                          >>{
                          >> std::istringstr eam iss(input.subst r(1));
                          >> iss >> amount;
                          >>}[/color]
                          >
                          >
                          > Perhaps excessive?
                          > if (cin.peek() == '$')
                          > cin.ignore(1);
                          >[/color]

                          I think there's a 0-argument ignore() for removing 1 character.

                          -Kevin
                          --
                          My email address is valid, but changes periodically.
                          To contact me please use the address from a recent posting.

                          Comment

                          Working...