strings and loops

This is a discussion on strings and loops within the C++ Programming forums, part of the General Programming Boards category; hey guys, I was hoping you could help me out with a problem I have. I am still kinda new ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    5

    strings and loops

    hey guys, I was hoping you could help me out with a problem I have. I am still kinda new at this and I wrote a program that required a password and then converted euros to dollars and vice versa. It works but when you tell it to give you euro to dollars or vice versa it doesnt give you a chance to input the number of dollars. here is the code, I dont know what code tags are but someone told me to put them on the code so here it is just plain. Thanks a lot! I also sent the .cpp if that is of any help. thanks again, max

    #include <iostream>
    using namespace std;
    int main (int argc, char * const argv[]) {

    string yesnomaybe;
    int x,y;
    cout<<"Please enter your password.";
    cin>>yesnomaybe;
    if (yesnomaybe == "skip") {
    cout<<"the password you entered is correct.";
    cout<<"would you like to covert euros to dollars or dollars to euros?";
    cin>>yesnomaybe;
    if (yesnomaybe == "euros to dollars")
    cout<<"please enter the amount of euros";
    cin>>x;
    cout<<"your euros in us dollars is "<<x*1.265<<endl;
    if (yesnomaybe == "Dollars to Euros please")
    cout<<"Please enter your amount of US Dollars";
    cin>>y;
    cout<<"Your US Dollars in Euros is "<<y/1.265;}


    else (yesnomaybe != "skip");
    cout<<"the password you entered is wrong try again";
    Attached Files Attached Files

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    There's a lot wrong with that code. First and foremost, the poor formatting (even in the attached file) hides your broken logic. Study the code that we post here and try to follow one of the styles. You'll find your errors much more easily.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    5
    could you just tell me whats wrong? The only problem I'm having is that when I tell it what to convert it doesnt give me a chance to enter the amount.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    well im no expert but i dont think you need to be to realise you wont get to many helpful answers until you rewrite your code...

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    5
    but how do I know what to write if no one tells me? here it is again.

    #include <iostream>
    using namespace std;
    int main (int argc, char * const argv[]) {

    string yesnomaybe;
    int x,y,;
    cout<<"Please enter your password.";
    cin>>yesnomaybe;
    if (yesnomaybe == "skip") {
    cout<<"the password you entered is correct.";
    cout<<"would you like to covert euros to dollars or dollars to euros?";
    cin>>yesnomaybe; }
    if (yesnomaybe == "euros to dollars")
    cout<<"please enter the amount of euros"; {
    cin>>x;
    cout<<"your euros in us dollars is "<<x*1.265<<endl; }
    if (yesnomaybe == "Dollars to Euros") {
    cout<<"Please enter your amount of US Dollars";
    cin>>y;
    cout<<"Your US Dollars in Euros is "<<y/1.265; }


    else (yesnomaybe != "skip");
    cout<<"the password you entered is wrong try again";

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Code tags... if you read the FAQs before posting you would have seen that they are required to be used. You read that and edit your posts (show that you care about this forum a bit to put forth some effort) and we will put forth some effort towards you.

  7. #7
    adr
    adr is offline
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Your code seemss to be missing "}{" around the "if"s this will case the program to mass up alot. Orgranizing your code would help too in the long run, it seems to kinda massy. Also your code says if yesnomaby = skip (1st time) to run the code, then at the end it says the same thing.... that will casue is I think. Also try changing "cin>>yesnomaybe" into "getline(cin, yesnomaybe);".

    LOL, sometimes I think poeple get to lazy XD and not like to help, like in lilhawk2892 post ^.^

    Prelude: Yea O.o; your right ^.^ that would help him more if he look at strings and formatting.

    Sorry for my bad spelling ^.^; i'm soo bad at it but, I hope I help out some. If you need more help on this, I can help out just PM me on here. Take care.
    Last edited by adr; 09-16-2006 at 05:19 PM.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    You are using string for one which require #include <string>

    Code:
    else (yesnomaybe != "skip");
    cout<<"the password you entered is wrong try again";
    
    //should be
    
    else   {     // you don't use values with else as it is fairly obvious that yesnomaybe is not = skip 
                    //   otherwise the else statement would not be being used
    cout<<"the password you entered is wrong try again";
    }
    Please use code tags
    Last edited by bumfluff; 09-16-2006 at 05:15 PM.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Also why not just use a different string for the input whether you want euros to dollars etc.

    From looking at your code you also show that you want to loop, a do while loop would be good for this. Look at teh cprogramming tutorial on loops.
    Last edited by bumfluff; 09-16-2006 at 05:16 PM.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    5
    thanks alot. also how and what are code tags

  11. #11
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Like I said read the FAQ it is in there

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >could you just tell me whats wrong?
    As you wish. To start with, the code in your post is incomplete so I looked at the attached file and my comments reflect that.

    >string yesnomaybe;
    This is wrong because you forgot to include <string>. The code won't compile.

    >int x,y,;
    This is wrong because there's an extra comma. The code won't compile.

    >cin>>yesnomaybe;
    This only reads a word and you're clearly expecting a line.

    >if (yesnomaybe == "euros to dollars")
    The body of this conditional is three lines, but you didn't add braces. That's broken logic.

    >cin>>x;
    This will generally fail because you expect a line of input but only read one word of it. Because the next word is unlikely to be a valid integer (judging by your conditional tests), cin will fail, x will remain uninitialized, and you've invoked undefined behavior by using it.

    >if (yesnomaybe == "Dollars to Euros please")
    The same problems exist with this conditional as well.

    >else (yesnomaybe != "skip");
    The semicolon at the end is the body of this conditional.

    >cout<<"the password you entered is wrong try again";
    This will always be printed.

    I could go on, but it's best to fix the compilation and logic errors before I give you a crash course in programming methodology and good style.
    My best code is written with the delete key.

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Prelude you did miss one point ( he is dividing a int by a float x/1.265

    oh I missed the I could go on in your post Prelude sorry

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Quote Originally Posted by Raigne
    Prelude you did miss one point ( he is dividing a int by a float x/1.265

    oh I missed the I could go on in your post Prelude sorry
    Actually, since there's no cast or trailing 'F', 1.265 is a double literal. Dividing an integer by a double will result in the integer being converted to a double before the operation. Since the result of the expression isn't assigned to an integer (it's passed to cout, which can handle doubles), there's nothing wrong with it at all.

    [edit]
    "Understanding is the key to intelligence, yet Knowledge is the key to Understanding" -Cody Doughty
    Therefore intelligence is the key to knowledge? [/edit]
    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.

  15. #15
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Ah it makes since now, and yes you are correct to the last part of that phrase.. You are prolly the first person to notice that, and also I guess it depends on your IDE on if that works, cause if I do it I get error: inproper coversion from int to float

Page 1 of 2 12 LastLast
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, 10:29 AM
  2. strings and loops...
    By twomers in forum C Programming
    Replies: 5
    Last Post: 12-12-2005, 10:28 AM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. Problems with strings and for loops
    By petedee in forum C Programming
    Replies: 52
    Last Post: 04-02-2004, 02:53 AM
  5. help with strings
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 09-21-2001, 02:15 AM

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