Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Loop in C+

  • 28-02-2011 5:24pm
    #1
    Registered Users, Registered Users 2 Posts: 275 ✭✭


    hey,
    i am tryin to write a c+ loop that will show the dollar and pound values for a range of euro values from 1 to 10? i am having alot of trouble getting my loop to work!
    any help would be greatly appreciated!


Comments

  • Moderators, Technology & Internet Moderators Posts: 1,336 Mod ✭✭✭✭croo


    it might be easier to get help if you posted your code so far.


  • Registered Users, Registered Users 2 Posts: 240 ✭✭Axe Rake


    Probably not a great example but it should give you some ideas about using an Array and for loops in c++
    #include <iostream>
    
    using namespace std;
    
    
    void convertToUSDollar(int euroArray[])
    {
        for(int i=0; i < 10; i++)
        {
            cout << euroArray[i] << " Euro(s) -> " << euroArray[i] * 1.38027 << " US Dollar(s)\n";
        }
    
    }
    
    void convertToBritishPound(int euroArray[])
    {
        for(int i=0; i < 10; i++)
        {
            cout << euroArray[i] << " Euro(s) -> " << euroArray[i] * 0.84897 << " British Pound(s)\n";
        }
    }
    
    
    int main(int argc, int *argv[])
    {
        int euroValues[] = {1,2,3,4,5,6,7,8,9,10};
    
        convertToBritishPound(euroValues);
        cout << "\n";
        convertToUSDollar(euroValues);
    
        return 1;
    }
    
    


  • Closed Accounts Posts: 5,082 ✭✭✭Pygmalion


    Axe Rake wrote: »
    Probably not a great example but it should give you some ideas about using an Array and for loops in c++

    inb4 he turns this in as his homework.


  • Closed Accounts Posts: 4,564 ✭✭✭Naikon


    Pygmalion wrote: »
    inb4 he turns this in as his homework.

    Does that code not qualify as well known though?

    I hope someday I can patent the main() function. Everybody would pay me royalties then.


  • Registered Users, Registered Users 2 Posts: 15,065 ✭✭✭✭Malice


    Naikon wrote: »
    I hope someday I can patent the main() function. Everybody would pay me royalties then.
    Pfft, WinMain() is where it's at these days :pac:


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 2,013 ✭✭✭SirLemonhead


    #include <iostream>
    #include <vector>
    
    void convertToUSDollar(const std::vector<int> &euroArray)
    {
        for (int i=0; i < euroArray.size(); i++)
        {
            std::cout << euroArray.at(i) << " Euro(s) -> " << euroArray.at(i) * 1.38027 << " US Dollar(s)\n";
        }
    }
    
    void convertToBritishPound(const std::vector<int> &euroArray)
    {
        for (int i=0; i < euroArray.size(); i++)
        {
            std::cout << euroArray.at(i) << " Euro(s) -> " << euroArray.at(i) * 0.84897 << " British Pound(s)\n";
        }
    }
    
    
    int main(int argc, char *argv[])
    {
    	std::vector<int> euroValues;
    
    	for (int i = 0; i < 10; i++)
    	{
    		euroValues.push_back(i+1);
    	}
    
        convertToBritishPound(euroValues);
        std::cout << "\n";
        convertToUSDollar(euroValues);
    }
    

    A bit more C++ ish :)


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    #include <iostream>
    #include <vector>
    
    void convertToUSDollar(const std::vector<int> &euroArray)
    {
        for (int i=0; i < euroArray.size(); i++)
        {
            std::cout << euroArray.at(i) << " Euro(s) -> " << euroArray.at(i) * 1.38027 << " US Dollar(s)\n";
        }
    }
    
    void convertToBritishPound(const std::vector<int> &euroArray)
    {
        for (int i=0; i < euroArray.size(); i++)
        {
            std::cout << euroArray.at(i) << " Euro(s) -> " << euroArray.at(i) * 0.84897 << " British Pound(s)\n";
        }
    }
    
    
    int main(int argc, char *argv[])
    {
    	std::vector<int> euroValues;
    
    	for (int i = 0; i < 10; i++)
    	{
    		euroValues.push_back(i+1);
    	}
    
        convertToBritishPound(euroValues);
        std::cout << "\n";
        convertToUSDollar(euroValues);
    }
    

    A bit more C++ ish :)
    ...
    for (int i=0; i < euroArray.size(); i++)
        {
            std::vector<int>::iterator IT = euroArray.begin();
            for (; IT != euroArray.end(); ++IT)
                  std::cout << *IT << " Euro(s) -> " << *IT * 1.38027 << " US Dollar(s)\n";
        }
    ...
    

    And a bit more C++ish again :)


  • Registered Users, Registered Users 2 Posts: 2,013 ✭✭✭SirLemonhead


    Webmonkey wrote: »
    ...
    for (int i=0; i < euroArray.size(); i++)
        {
            std::vector<int>::iterator IT = euroArray.begin();
            for (; IT != euroArray.end(); ++IT)
                  std::cout << *IT << " Euro(s) -> " << *IT * 1.38027 << " US Dollar(s)\n";
        }
    ...
    

    And a bit more C++ish again :)

    I knew someone would be along to add iterators :)


  • Registered Users, Registered Users 2 Posts: 1,421 ✭✭✭Steveire


    fcleere wrote: »
    i am tryin to write a c+ loop

    I love the irony of an early termination while writing "C++".
    Webmonkey wrote: »
    And a bit more C++ish again :)[/QUOTE]
    
    [code]
    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    struct USDPrinter
    {
        void operator()(int input) {
          std::cout << input << " Euro(s) -> " << input * 1.38027 << " US Dollar(s)\n";
        }
    };
    
    struct GBPPrinter
    {
        void operator()(int input) {
          std::cout << input << " Euro(s) -> " << input * 0.84897 << " British Pound(s)\n";
        }
    };
    
    template<typename Converter>
    void printConversion(const std::vector<int> &euroArray)
    {
        std::for_each(euroArray.begin(), euroArray.end(), Converter());
    }
    
    int sequenceGenerator() {
      static int i = 0;
      return ++i;
    }
    
    int main(int argc, char *argv[])
    {
        std::vector<int> euroValues(10);
    
        std::generate(euroValues.begin(), euroValues.end(), sequenceGenerator);
    
        printConversion<GBPPrinter>(euroValues);
        std::cout << "\n";
        printConversion<USDPrinter>(euroValues);
    }
    

    Now who's going to do it with lambdas?


  • Closed Accounts Posts: 4 fabioi


    If you are interested.
    #include <stdio.h>
    
    #define DOLLAR_RATE 1.38027
    #define POUND_RATE  0.84897
    
    int main( int argc, char *argv[] )
    {
        int euroValues[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int *p = euroValues;
        unsigned int i;
        unsigned int arraySize = ((sizeof(euroValues)/sizeof(euroValues[0])) - 1);
    
        for(i = 0, p = euroValues; i < arraySize; p++, i++) {
            printf("US$ %.2f\n", (*p * DOLLAR_RATE));
        }
    
        for(i = 0, p = euroValues; i < arraySize; p++, i++) {
            printf("&#163;%.2f\n", (*p * POUND_RATE));
        }
    
        return 0;
    }
    


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,304 ✭✭✭oneofakind32


    Wow this thread didnt take long to turn into a pissing contest did it? Poor OP is probably more confused then ever by all yeer code!


  • Registered Users, Registered Users 2 Posts: 1,421 ✭✭✭Steveire


    Wow this thread didnt take long to turn into a pissing contest did it? Poor OP is probably more confused then ever by all yeer code!
    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    #include <boost/lambda/lambda.hpp>
    
    using namespace boost::lambda;
    
    int main(int argc, char *argv[])
    {
      std::vector<int> euroValues(10);
      {
        int i = 0;
        std::generate(euroValues.begin(), euroValues.end(), ++var(i));
      }
    
      std::for_each(euroValues.begin(), euroValues.end(),
                    std::cout << _1 << " Euro(s) -> "
                              << _1 * 0.84897 << " British Pound(s)\n" );
      std::cout << std::endl;
      std::for_each(euroValues.begin(), euroValues.end(),
                    std::cout << _1 << " Euro(s) -> "
                              << _1 * 1.38027 << " US Dollar(s)\n" );
    }
    

    Here I end the madness for my part anyway because my compiler doesn't support C++0x lambdas.


  • Closed Accounts Posts: 3,357 ✭✭✭Beano


    :mad:

    Why are people so keen to do homework for somebody who hasnt even shown that they have made an effort for themselves? You're not helping the OP at all. All it does is lead to people leaving college with a computer degree but unable to programme themselves out of a wet paper bag.


  • Closed Accounts Posts: 4,564 ✭✭✭Naikon


    Beano wrote: »
    :mad:

    Why are people so keen to do homework for somebody who hasnt even shown that they have made an effort for themselves? You're not helping the OP at all. All it does is lead to people leaving college with a computer degree but unable to programme themselves out of a wet paper bag.

    Like most computer degrees imo. Having a degree in computer science proves you can pass exams related to the profession.
    Does not make you competant, though. Why do you think medical school exists? You have to prove your worth in the profession.
    A degree is the first potential step in this process. It's not uncommon for people without a degree to be good at Software Development.


  • Registered Users, Registered Users 2 Posts: 5,246 ✭✭✭conor.hogan.2


    This may be homework - but its probably not. Either way, you are not helping so please be quiet.

    If they cant program and employer or anyone can spot it quickly and thats that, having a degree doesnt magically pass you through an interview.


  • Registered Users, Registered Users 2 Posts: 15,065 ✭✭✭✭Malice


    Naikon wrote: »
    Like most computer degrees imo. Having a degree in computer science proves you can pass exams related to the profession.
    Does not make you competant, though. Why do you think medical school exists? You have to prove your worth in the profession.
    A degree is the first potential step in this process. It's not uncommon for people without a degree to be good at Software Development.
    While I agree to a certain extent with what you've written I think you've missed Beano's point slightly. The OP posted what is almost certainly a homework question, didn't proofread what they had written to correct a glaring typo in the language name which they subsequently repeated, hasn't responded to any questions that were asked and hasn't returned in the intervening four days to check if their question was answered.

    Now maybe I'm jumping to conclusions but all of those factors suggest to me that the OP isn't too concerned with learning where they are going wrong.
    Just posting code for them is unlikely to help them learn as much as them posting whatever they had attempted and having it corrected. If they progress through their degree and manage to secure a job after graduation will they still be expecting the likes of us to write their code for them?


  • Registered Users, Registered Users 2 Posts: 15,065 ✭✭✭✭Malice


    If they cant program and employer or anyone can spot it quickly and thats that, having a degree doesnt magically pass you through an interview.
    You'd be surprised! I've worked with some people and I couldn't understand how they were hired as they were clearly unable for the role. Some places don't put as much emphasis on the technical aspects of an interview so if you can impress some drone from HR who won't understand any techno-talk you can probably go quite far.


  • Registered Users, Registered Users 2 Posts: 5,246 ✭✭✭conor.hogan.2


    Well then, I should be getting a decent job. Happy days ;).

    I agree it is probably "homework" - but posting saying everyone should not respond is not helpful to anyone.

    Actually thats a good point, most companies there will be a non tech person interviewing you.


  • Registered Users, Registered Users 2 Posts: 15,065 ✭✭✭✭Malice


    I agree it is probably "homework" - but posting saying everyone should not respond is not helpful to anyone.
    It probably wasn't clear from my post but what I meant was that the OP should have posted their attempt so that others could correct what they had done rather than spoonfeeding them a ready-made solution. I also reckon that's what Beano meant.


  • Registered Users, Registered Users 2 Posts: 5,246 ✭✭✭conor.hogan.2


    Ideally, yes. Beanos post did little to help anything though and is more suited to irc or a response on stack overflow (rightly so there though).


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    The OP didn't bother coming back either so I take it, it was homework. Got what they needed and gone :p

    Ah pretty obvious it was from first post though.


  • Registered Users, Registered Users 2 Posts: 5,246 ✭✭✭conor.hogan.2


    But making a post all :mad::mad::mad: adds nothing, homework or no homework.


  • Closed Accounts Posts: 3,357 ✭✭✭Beano


    This may be homework - but its probably not. Either way, you are not helping so please be quiet.

    If they cant program and employer or anyone can spot it quickly and thats that, having a degree doesnt magically pass you through an interview.


    I'll have my say when i feel like it. i dont need your permission.


  • Closed Accounts Posts: 3,357 ✭✭✭Beano


    But making a post all :mad::mad::mad: adds nothing, homework or no homework.

    it was one :mad: not 3. learn to count.


  • Registered Users, Registered Users 2 Posts: 275 ✭✭fcleere


    well that was entertaining!!
    sorry about not getting back to ye sooner gents/ladies, but for some strange reason i did not receive any email notifications until today! i also managed to figure out where i was going wrong and got the code working so never thought of checking back.
    apologies for my poor post, and apologies for not posting my code attempts. a glaring omission on my behalf.
    there is my completed code, should it interest any of ye. the code was for a lab i was doing.
    thanks to all the people who posted helpful replies.
    now we can all unclench our sphincters and relax.
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>


    int main(void)
    {


    int i;
    float E,GBP,USD,C;

    printf("Euro\tGBP\tUSD\n");
    {
    GBP=0.623;
    USD=1.293;
    for(i=1;i<11;i++)


    printf("%d\t %.3f\t %.3f\t\n",i,GBP*i,USD*i);
    }




    printf("Welcome to the Currency Convertor Program\n");

    printf("Enter the desired amount in Euro\n");
    scanf("%f", &E);
    printf("Please enter the desired currency, GBP(1) or USD(2)\n");
    scanf("%f",&C);

    if ("%f",C==1)
    {

    GBP = (E*0.623);
    printf("%9.3f GBP\n",GBP);
    }

    if ("%f",C==2)
    {
    USD= (E*1.293);
    printf("%9.3f USD\n",USD);
    }

    if ("%f",C>2||C<1)
    {
    printf("Invalid input, please try again\n");
    }


    printf ("Thank you for using the Currency Convertor Program\n");
    printf ("Have a nice day.\n");
    return (EXIT_SUCCESS);
    }


Advertisement