Thread: Need help with this code

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    72

    Need help with this code

    So a bit of what I am trying to do. I am trying to make a program that ask you for name and gender (age is always used lets use gender be deferent, and yes I hate doing hello world for the same reason )
    and then ask if the information is correct and if it's not to redo it all.


    Code:
    #include <iostream> 
    #include <cstdlib>
    
    using namespace std;
    
    int  main()
    {
     string my_name;
     string my_gender;
     string yes_no;
    
         
         
       cout<<"Welcome please type your name \n";
          cin>> my_name;
    
          cout<<"Hello "  <<  my_name <<  " and what gender are you?";
          cin>>my_gender;
          cout<< "You are " << my_name << "and are a " << my_gender << "? /n";
          cout<< "type yes or no";
          cin>> yes_no;

    After this line I had a few plans to make this work
    A. loop if yes_no = no
    B. loop while yes_ no = no

    Looking at them I think they are the same thing but I could be wrong
    and even so is one "bad programing" if so why?
    Can strings even work this way? I see no reason why not but the error is making me think other wise, it says "no" is not declared but it should be with cin>> yes_no right?

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Sorry, look below...
    Devoted my life to programming...

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You need a do while loop:

    Code:
    do
    {
        // your code here
    }while (/* conditions */);
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    72
    Never would of thgout do while. can you exspan why it says "no" is not declared? Do i need a defreint way to let them choice betwen yes and no then strings?

    Code:
    #include <iostream> 
    #include <cstdlib>
    
    using namespace std;
    
    int  main()
    {
     string my_name;
     string my_gender;
     string yes_no; //this is the no they say is undeclared//
    
         
         do {
         cout<<"Welcome please type your name \n";
          cin>> my_name;
    
          cout<<"Hello "  <<  my_name <<  " and what gender are you?";
          cin>>my_gender;
          cout<< "You are " << my_name << "and are a " << my_gender << "? /n";
          cout<< "type yes or no";
          cin>> yes_no;
          } while (yes_no = no);
    Last edited by Shawn Belcher; 09-19-2011 at 11:48 PM.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    "Normal text is treated as code by the compiler. If you want any text to be a string literal (that is anonymous string data) you have to surround it in double quotes. For example, this entire post is treated as one string literal by C++, because of the double quotes. You could compare this post with a string."

    "Not to mention that your do-while loop is also wrong because you're using assignment instead of an equally comparison (that is ==)."

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    '=' is for assignment, '==' is for comparison.

    enclose 'no' in double quotes: "no"

    Alltogether: }while (yes_no == "no");
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    72
    It works thanks. But why do while acording to your tut on it
    DO..WHILE loops are useful for things that want to loop at least once
    but how is that defreint fomr while in this case?
    as the loop is not decide intill you use the yes_no string do you need one that just make sure it gose though once? or am I missing a bigger pitcure0?

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It really baffles me how we occasionally get people who have horrifically bad English spelling and grammar skills, that decide to take up programming.
    I'm not really trying to insult, though I'm sure it will at least partially come off that way. I'm at least going to provide help below as well...
    Is this programming out of neccessity, or is it a mixup of priorities, or did you sign up for the wrong class or something? I really would suggest trying to work on it because English, or at least some first language, is an important part of the skills of a programmer, particularly when it comes to UI design.

    Well anyway, there are two problems with your while condition.
    #1 is that you need == for comparison, not just =, which is actually for assignment.
    #2 is that in order for no to be a string, it needs to be surrounded in double-quotes like this: "no"

    Edit: Man I really should reload the page before posting.
    A while loop will not necessarily execute even once. For example if I said "while you are not sitting, clap your hands" then you would probably not need to clap your hands at all because you are probably sitting already.
    On the other hand if I said "clap you hands, and then continue clapping if you are not sitting" then that would be like a do..while loop, because you had to clap once regardless of whether you were sitting or not.
    Last edited by iMalc; 09-20-2011 at 12:52 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    72
    Well if you think about it if you suck at one langue learn another right?
    For real how ever, I belive my skills are good enough to write C++.


    Is this programming out of neccessity, or is it a mixup of priorities, or did you sign up for the wrong class or something?
    I'm unsure what you mean by "mixup of priorites", did not sign up for a class, and its not out of neccessity.
    Last edited by Shawn Belcher; 09-20-2011 at 12:57 AM.

  10. #10
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Shawn Belcher View Post
    Well if you think about it if you suck at one langue learn another right?
    Not if the later somehow depends on the former.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Shawn Belcher View Post
    For real how ever, I belive my skills are good enough to write C++.
    Optimism is a wonderful thing.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  12. #12
    Registered User
    Join Date
    Sep 2011
    Location
    UK
    Posts
    5
    Code:
     
    do {code}while(conditional)
    
    while (conditional){code}
    The first, executes the code block, and then checks the conditional in order to see whether to execute again. Thereby insuring at least one repetition of the loop.

    The second, checks the conditional first; if true executes the block, if false nothing happens and the flow of control moves to the next available statement.




    As an aside, if this is your first time with a programming language, you may want to consider learning something like Java first. That will give you a firm understanding of OO concepts, all the control structures and design considerations without nearly as steep a learning curve.
    A lot of educational establishments nowadays take this route prior to C++; and personally speaking that's what I did myself.
    Last edited by TastySauceCode; 09-20-2011 at 06:30 AM.

  13. #13
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by TastySauceCode View Post
    ...........without nearly as steep a learning curve.
    You mean..

    Code:
    class HelloWorldApp 
    {
         public static void main(String[] args) 
        {
             System.out.println("Hello World!");
         } 
    }
    is simpler than


    Code:
    #include<iostream>
    int main()
    {
        std::cout<<"Hello World !";
        return 0;
    }
    ?

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by TastySauceCode View Post
    As an aside, if this is your first time with a programming language, you may want to consider learning something like Java first. That will give you a firm understanding of OO concepts, all the control structures and design considerations without nearly as steep a learning curve.
    A lot of educational establishments nowadays take this route prior to C++; and personally speaking that's what I did myself.
    I have worked with (and managed) people who learnt Java as a stepping stone to C++. Most of them require extensive coaching to ween them from techniques that, although alright in Java, are really bad ideas in C++.

    Generally speaking, if you want to learn any language, you should focus on learning that language. Not try to learn one language first, and then the next language by adapting what you learned with the first. If moving from any language to another, one of the major steps is unlearning techniques from the first language before you can properly use the second language.

    For that reason, I generally advise against learning C if you want to learn C++, advise against learning C++ if you want to know C, advice against learning Java as a stepping stone to C++, or advise against learning C++ and then using that knowledge as a basis for learning Java.

    That is not suggesting any of the languages are better or worse than the others. But the human mind finds it easier to learn good habits than to unlearn bad habits.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  15. #15
    Registered User
    Join Date
    Sep 2011
    Location
    UK
    Posts
    5
    Quote Originally Posted by manasij7479 View Post
    You mean..

    Code:
    class HelloWorldApp 
    {
         public static void main(String[] args) 
        {
             System.out.println("Hello World!");
         } 
    }
    is simpler than


    Code:
    #include<iostream>
    int main()
    {
        std::cout<<"Hello World !";
        return 0;
    }
    ?

    Heh, with a program as trivial and on such a small scale as 'Hello World', there isn't much difference granted. However, when things get more larger in scope and C++ introduces memory management, pointers* etc. than the added complexity becomes quite apparent. Obviously though Java pays for this 'advantage' with less performance.

    *Although I guess it could be argued that Java references are watered down C++ pointers...




    Quote Originally Posted by grumpy View Post
    I have worked with (and managed) people who learnt Java as a stepping stone to C++. Most of them require extensive coaching to ween them from techniques that, although alright in Java, are really bad ideas in C++.

    Generally speaking, if you want to learn any language, you should focus on learning that language. Not try to learn one language first, and then the next language by adapting what you learned with the first. If moving from any language to another, one of the major steps is unlearning techniques from the first language before you can properly use the second language.

    For that reason, I generally advise against learning C if you want to learn C++, advise against learning C++ if you want to know C, advice against learning Java as a stepping stone to C++, or advise against learning C++ and then using that knowledge as a basis for learning Java.

    That is not suggesting any of the languages are better or worse than the others. But the human mind finds it easier to learn good habits than to unlearn bad habits.
    Ah, good points. I think I may be guilty of not wording my original post carefully enough. Someone shouldn't think that by learning Java they are a mere stones throw away from C++. Getting to grips with it takes a lot of knuckling down, and often you'll be going over something that you thought had nailed in Java but now requires a whole new approach or way of thinking.
    That being said the general ideas of OOP, control structures etc. and the logic on which they operate are the same. And maybe it's just me but the ease of the syntax made learning and testing these concepts both surprisingly quick and enjoyable.
    And Put it this way, even without prior programming experience, most could have a very good grip on Java within a week or two. And that's probably why it's so popular as the language most people cut their teeth on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  2. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  3. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 07:09 AM
  4. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM