strings and loops

This is a discussion on strings and loops within the C++ Programming forums, part of the General Programming Boards category; Your compiler, you mean. The IDE has nothing to do with whether code compiles or not; it's just the fancy ...

  1. #16
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Your compiler, you mean. The IDE has nothing to do with whether code compiles or not; it's just the fancy interface.

    Are you referring to me? You get an error if you attempt to assign a float to an int? Right, so do I. If you want to assign a floating-point number to an integer, you can use a cast:
    Code:
    int x = (int)(3.14159);
    That truncuates the value, however; it doesn't round it. To round the value, add .5 first:
    Code:
    int x = (int)(3.14159 + .5);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #17
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Ah yes, instead of

    Code:
    cin >> yesnomaybe
    
    //you should have
    
    getline (cin, yesnomaybe);
    That will read the whole line of input rather than just one character.

    But I think what you should do is look at the errors that you get when compiling it as that should help you to understand what is wrong.

  3. #18
    adr
    adr is offline
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    lmao, thats what I said earlyer .... wow... did you not all read my post O.o; here what it would look like if hew would change it...>.> by going what I said.... and no you dont need a string thing to it really... not in my Dev-C++ anyways...
    Code:
    #include <iostream>
    
    using namespace std;
    int main(void)
    {
     string yesnomaybe;
     int x,y;
    
      cout<<"Please enter your password."<<endl;
      getline(cin, yesnomaybe);
    	if(yesnomaybe == "skip"){
    	    cout<<"the password you entered is correct."<<endl;
    	    cout<<"would you like to covert euros to dollars or dollars to euros?"<<endl;
                        getline(cin, yesnomaybe);
            if(yesnomaybe == "euros to dollars"){
                     cout<<"please enter the amount of euros"<<endl;
    	             cin>>x;
    	             cout<<"your euros in us dollars is "<<x*1.265<<endl;
                                 system("pause");}
    	    else if(yesnomaybe == "Dollars to Euros please"){
    	              cout<<"Please enter your amount of US Dollars"<<endl;
                                  cin>>y;
                                  cout<<"Your US Dollars in Euros is "<<y/1.265<<endl;
                                  system("pause");}}
    	else if(yesnomaybe == "pie"){
    	             cout<<"the password you entered is wrong try again"<<endl;}
    }
    For some reason the site massing up how my code it set up somewhat (its adding spaces and/or taking away spaces) so sorry for that.
    Last edited by adr; 09-17-2006 at 02:44 AM.

  4. #19
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Well you need to include <string> and you also use system("pause") which is unportable and requires #include <cstdlib>.

    Surely the last else if shouyld be just an else because the password might not be pie.

    Not sure if this is right, but could there be some problems with getting input to yesnomaybe again before setting it to NULL?

  5. #20
    adr
    adr is offline
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    LMAO wow, you dont really need em in Dev-C++ like I said dude. Did you not read again XD I know my code works so you really cant say it doesnt. It was only a demo of how the code should work, not a fulll thought out program O.o; I would make it hack prof some then XD. How a program compiles is differnt on eatch compiler like some one said a bit ago. Yes you would need to add "#include <string>" and system("pause") is a program code all ready ^.^; Here so you will be happy lmao.
    #include <iostream>
    #include <string>
    #include <cstdlib>
    using namespace std;
    int main(void)
    {
    string yesnomaybe;
    int x,y;

    cout<<"Please enter your password."<<endl;
    getline(cin, yesnomaybe);
    if(yesnomaybe == "skip"){
    cout<<"the password you entered is correct."<<endl;
    cout<<"would you like to covert euros to dollars or dollars to euros?"<<endl;
    getline(cin, yesnomaybe);
    if(yesnomaybe == "euros to dollars"){
    cout<<"please enter the amount of euros"<<endl;
    cin>>x;
    cout<<"your euros in us dollars is "<<x*1.265<<endl;
    system("pause");}
    else if(yesnomaybe == "Dollars to Euros please"){
    cout<<"Please enter your amount of US Dollars"<<endl;
    cin>>y;
    cout<<"Your US Dollars in Euros is "<<y/1.265<<endl;
    system("pause");}}
    else{
    cout<<"the password you entered is wrong try again"<<endl;}
    }
    Wow now its taking away all my spaces ^.^ nice. LOL. Also if you like to have a loop, here.

    #include <iostream>
    #include <string>
    #include <cstdlib>
    using namespace std;
    int main(void)
    {
    string yesnomaybe;
    int x,y;
    bool pQuit = false;
    while( false == pQuit )
    {
    cout<<"Please enter your password."<<endl;
    getline(cin, yesnomaybe);
    if(yesnomaybe == "skip"){
    cout<<"would you like to covert euros to dollars or dollars to euros?"<<endl;
    getline(cin, yesnomaybe);
    if(yesnomaybe == "euros to dollars"){
    cout<<"please enter the amount of euros"<<endl;
    cin>>x;
    cout<<"your euros in us dollars is "<<x*1.265<<endl;
    system("pause");
    else if(yesnomaybe == "Dollars to Euros please"){
    cout<<"Please enter your amount of US Dollars"<<endl;
    cin>>y;
    cout<<"Your US Dollars in Euros is "<<y/1.265<<endl;
    system("pause");}}
    else{
    cout<<"the password you entered is wrong try again"<<endl;}
    }
    }
    Last edited by adr; 09-17-2006 at 03:19 AM.

  6. #21
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    LMAO wow, you dont really need em in Dev-C++ like I said dude. Did you not read again XD I know my code works so you really cant say it doesnt.
    Your code "works" by relying on nonstandard features specific to your compiler. It would not compile on other standards-conforming compilers. Your code works because inside Dev-C++'s version of <iostream> it internally has an #include <string> -- something you can NOT guarantee in standard C++.

    For example, your original code will NOT compile on VC++, because in VC++, <iostream> does not include <string>. Since your code does not work on all standards-conforming compilers, yes I do say it doesn't work.

    How a program compiles is differnt on eatch compiler like some one said a bit ago.
    Which is why it is very important to write in standard C++, and not take advantage of compiler quirks. If your code is fully standard C++ it should compile and execute properly on every standards-conforming compiler.

    Without the #include <string>, it will compile on some compilers (but fail on others). With it, it will compile on all standards-conforming compilers.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  7. #22
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Well in my version of Dev-C++ I needed them so there

    The problem with your loop code is that there is no way of breaking out of the loop.

    I think steelydan you should forget the code that you started with, yes with some tweaking it will work, but I would just get a pen and paper and think about how you could make your program more user friendly.

    E.g Perhaps you would want to change the entering of the currency change you want to perform:

    Code:
    int selection;
    cout << "1. Euros To Dollars\n2. Dollars To Euros\n\n";
    cout << "Please make your selection: ";
    cin >> selection;
    That will make it a lot easier for people to know what to do as you don't tell them anywhere that they should type euros to dollars.

    Yes I know this program is probably only for your use but it is a good habit to get into, as it wll make more people want to use your programs in the future.

  8. #23
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >yes with some tweaking it will work
    It takes a surprising amount of tweaking. I agree that scrapping it and starting over with a good reference is an excellent idea though.
    My best code is written with the delete key.

  9. #24
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    It wouldn't tale that much to tweak it to work, but I mean work in a losse round-about way.

  10. #25
    adr
    adr is offline
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Quote Originally Posted by bumfluff
    Well in my version of Dev-C++ I needed them so there

    The problem with your loop code is that there is no way of breaking out of the loop.
    Did you mean mine O.o;? Yea its easy to break out of it... just have to add in something for it that truns bool pQuit = false; into bool pQuit = true; witch is a really simple loop way to do things. I know theres more any one way of doing anything.

    Your code "works" by relying on nonstandard features specific to your compiler. It would not compile on other standards-conforming compilers. Your code works because inside Dev-C++'s version of <iostream> it internally has an #include <string> -- something you can NOT guarantee in standard C++.

    For example, your original code will NOT compile on VC++, because in VC++, <iostream> does not include <string>. Since your code does not work on all standards-conforming compilers, yes I do say it doesn't work.
    Its why made the 2 one so ppl wouldnt really have to change much in it. I know my 1st code wont work on other complies most likely, never said it would. I was just meaning, you really dont have to "add" that code in there really. Yes if you want it to be standard, or want if your compile cant do that.

    Which is why it is very important to write in standard C++, and not take advantage of compiler quirks. If your code is fully standard C++ it should compile and execute properly on every standards-conforming compiler.

    Without the #include <string>, it will compile on some compilers (but fail on others). With it, it will compile on all standards-conforming compilers.
    Yes I go with that 100% but as I said befor, or was trying to say, in my Dev-c++ I didnt have to... (lol even said it was just a demo on how it works... the idea) I do add it when I make a real program-code, but for this I didnt.

  11. #26
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Why didn't you, because it would only serve to confuse him more by showing him correctish code that still didn't work.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loops with incrementing strings .... ?
    By twomers in forum C++ Programming
    Replies: 1
    Last Post: 12-12-2005, 11:29 AM
  2. strings and loops...
    By twomers in forum C Programming
    Replies: 5
    Last Post: 12-12-2005, 11:28 AM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 07:37 AM
  4. Problems with strings and for loops
    By petedee in forum C Programming
    Replies: 52
    Last Post: 04-02-2004, 03:53 AM
  5. help with strings
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 09-21-2001, 03:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21