Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

This program I have to do

  • 12-02-2006 06:52PM
    #1
    Closed Accounts Posts: 1,299 ✭✭✭


    so this is the c++ program I have to make
    Sample interaction (but don’t feel totally constrained by this):
    Enter a company code, and number of shares held: BOI 100
    Enter the day month and year of purchase of these shares: 3 7 2005
    100 BOI purchased for £2015.00, today worth £2022.00, gain of 3.47%
    Do you have more shares? Y
    Enter a company code, and number of shares held: FFA 350
    Enter the day month and year of purchase of these shares: 22 11 1999
    350 FFA purchased for £7112.00, today worth £7077.00, loss of 0.5%
    Do you have more shares? N
    Total purchase price was £9127.00.
    Current value is £9099.00.
    Loss since purchase of £28.00 or 0.3% of £9127.00

    And I have until where theyask ou "Do you have more shares" bit,
    how do i set it up to recognise the y for yes or n for no if you get me

    I need to do a while loop that will keep looping while the value is y(as in yes),
    but how do I set that up?
    is it some kind of character as in while (x==n),

    I'm sure it's easy but I cant figure it out.


Comments

  • Registered Users, Registered Users 2 Posts: 19,396 ✭✭✭✭Karoma


    You didn't even specify which language... gah!


    Read in a Char from the keyboard...
    Look up "loops" do While or if or.. there's a dozen ways to do it


    Is it that you don't know how to read input from a keyboard? Or that you don't know about different types? Or that you don't know loops?
    What year are you in ?
    oO


  • Closed Accounts Posts: 1,299 ✭✭✭Sandals


    sorry that was stupid of me it's c++


  • Closed Accounts Posts: 3,285 ✭✭✭Smellyirishman


    My C++ is none existant so bear with me.

    Very simplistically your looking at

    char input = 'y'

    while( input == 'y' || input == 'Y')
    read in shares
    std::cout << "Do you have more"
    std::cin >> input

    finish up


  • Closed Accounts Posts: 1,299 ✭✭✭Sandals


    THESE THREE LINES ARE NOT WORKING;

    cout << "Do You have more shares (y/n)?";
    cin >> x;
    while (x == "y"){


  • Registered Users, Registered Users 2 Posts: 7,544 ✭✭✭BrokenArrows


    they should work. Its perfect code. lol

    Anyway what do you mean by not working?
    Not doing what you want?
    Not compiling?


  • Advertisement
  • Closed Accounts Posts: 3,285 ✭✭✭Smellyirishman


    You cannot test equivilance on Strings using ==, you must use a function.
    I think the function in C++ is Compare(String A, String B, bool ignoreCase). This function returns a float depending on whether the string is "lesser" ( returns < 0 I think) or "greater" ( > 0), if they are equal then 0 is returned.

    So in order to check if the char entered ( x in your case ) is equal to 'y' then you must use the single character quotes. IE;

    while ( x == 'y' )

    using a string it would be

    while ( Compare(x,"y",true) == 0 )

    Did you import the libraries for IO? Try using std::cin and std::cout instead.

    Again, take this with a pinch of salt.


  • Closed Accounts Posts: 1,299 ✭✭✭Sandals


    I dont know how to import the libraries for Io,
    what should I call x,
    is it an int or a char
    like
    int x;
    or char x[2];

    ? thanks for your help.


  • Closed Accounts Posts: 1,299 ✭✭✭Sandals


    This is what I have, and some where here the problem lies, the rest of program works fine;

    char companycode[80],t[80];
    int number_shares,day,month,year,pday,pmonth,pyear;
    cout << "Do You have more shares (y/n)?";
    cin >> t;


    while ( t == 'y' ){


  • Closed Accounts Posts: 3,285 ✭✭✭Smellyirishman


    K, well I don't want to give too much away...

    Here is the entire, 100% working code to do a while loop reading in "shares" until the user enters something other than y.
    It's very basic and straight forward, if your stuck on any of it say so.
    #include  <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main()
    {
        int share = 0;
        char input = 'y';
        
        while( 'y' == input ) 
        { 
               cout << "Please enter new share value  ";
               cin >> share;
               cout << endl;
               cout << "Valude entered was : " << share;
               cout << endl;
               cout << "Do you want to enter more shares?";
               cin >> input;
        }
        
        return 0;
    }
    


Advertisement