Need Help with writing this Program!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Need Help with writing this Program!

  1. #1
    Join Date
    Sep 2010
    Posts
    4

    Need Help with writing this Program!

    Hey guys!

    I'm new to coding and having a difficulty. I am suppose to create a program in Visual Studio. Here's the instructions:


    Write a program that asks the user to enter either character a or b from the keyboard. If the user enters a then prompt the user to input three floating- point numbers and print the sum and average, of three numbers. If the user enters b, then prompt the user to enter three initials of first name, middle name, and last name Print the initials. The screen dialogue should appear as follows. . Hint: You need to use if statement i.e.

    if x > y
    statement1;
    else
    statement2;

    Enter character a or b: a
    Enter three floating-point numbers: -3 5 8

    Sum = 10
    Average = 3.33

    Or
    Enter character a or b: b

    Enter first initial: N
    Enter first initial: B
    Enter first initial: C

    Your initial is: NBC





    Here's what I have so far. I believe my if and else area is not right! Please help me out!



    #include <iostream>

    using namespace std;

    int main ()

    {

    float number1, number2, number3;

    char ch1='a', ch2='b';

    int sum, average;

    cout <<"Please enter a or b"

    cin >> ch1 >> ch2;

    if (ch1=='a')

    cout <<"Please input the three floating point numbers and print the sum and average of three numbers"

    else cout<<"Please enter three initials of your first name, middle name, and last name"

    return 0;
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,046

    Re: Need Help with writing this Program!

    You only need to input a or b into one char.

    Check if it's equal to a then prompt for the numbers and compute and display the output. That should all be contained inside one code block.

    If it's not equal to a, check if it's equal to b. If it is, prompt for and print the initials. Again, all that will be in one code block.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Need Help with writing this Program!

    Let's break up the instructions into pseudocode a bit....

    Code:
    //Write a program
    #include <iostream>
    using namespace std;
    
    int main()
    {
        //that asks the user to enter either character ‘a’ or ‘b’ from the keyboard.
        INPUT choice
    
        if (the user enters ‘a’)
        {
            //then prompt the user to input three floating- point numbers and print the sum and average, of three numbers.
        }
        else if (the user enters ‘b’)
        {
            //then prompt the user to enter three initials of first name, middle name, and last name Print the initials.
        }
    
        return 0;
    }
    As for this bit:
    "The screen dialogue should appear as follows."
    that means they're telling you precisely how you should be formatting your output and input prompts at each step, so all that's left is storing the inputs appropriately and then manipulating them appropriately.

  4. #4
    Join Date
    Sep 2010
    Posts
    4

    Re: Need Help with writing this Program!

    Quote Originally Posted by GCDEF View Post
    You only need to input a or b into one char.

    Check if it's equal to a then prompt for the numbers and compute and display the output. That should all be contained inside one code block.

    If it's not equal to a, check if it's equal to b. If it is, prompt for and print the initials. Again, all that will be in one code block.
    I'm sorry but I'm bit confused. Can u show me what you are talking about by fixing it in my script?

  5. #5
    Join Date
    Sep 2010
    Posts
    4

    Re: Need Help with writing this Program!

    Quote Originally Posted by Lindley View Post
    Let's break up the instructions into pseudocode a bit....

    Code:
    //Write a program
    #include <iostream>
    using namespace std;
    
    int main()
    {
        //that asks the user to enter either character a or b from the keyboard.
        INPUT choice
    
        if (the user enters a)
        {
            //then prompt the user to input three floating- point numbers and print the sum and average, of three numbers.
        }
        else if (the user enters b)
        {
            //then prompt the user to enter three initials of first name, middle name, and last name Print the initials.
        }
    
        return 0;
    }
    As for this bit:
    "The screen dialogue should appear as follows."
    that means they're telling you precisely how you should be formatting your output and input prompts at each step, so all that's left is storing the inputs appropriately and then manipulating them appropriately.
    Thanks Lindley. So should I start my script with what u stated in your code?

    I really need the final output of how everything will be arranged.

    Thanks again!

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,046

    Re: Need Help with writing this Program!

    Quote Originally Posted by Constantine! View Post
    I'm sorry but I'm bit confused. Can u show me what you are talking about by fixing it in my script?
    Did Lindley's post help? Nobody's going to actually do the work for you. We'll nudge you in the right direction.

    Look at your first cin statement. You should only input into one variable, then evaluate that for a or b.

    FWIW, program and script are not interchangeable terms.

  7. #7
    Join Date
    Sep 2010
    Posts
    4

    Re: Need Help with writing this Program!

    Quote Originally Posted by GCDEF View Post
    Did Lindley's post help? Nobody's going to actually do the work for you. We'll nudge you in the right direction.

    Look at your first cin statement. You should only input into one variable, then evaluate that for a or b.

    FWIW, program and script are not interchangeable terms.
    I saw lindley's post after I was replying to first reply and yea it helped alot. I have a good foundation on what I am doing right and wrong. Just need help in compiling it to final output!

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Need Help with writing this Program!

    I'll break it down a little further, but beyond that you're going to have to show some capability to take my outline and run with it yourself.
    Code:
    //Write a program
    #include <iostream>
    using namespace std;
    
    int main()
    {
        //that asks the user to enter either character a or b from the keyboard.
        INPUT choice
        // You'll need a variable to hold the input---perhaps call it "choice". A char will do. You'll also need a cin statement.
    
        if (the user enters a)// you are testing the value of the choice variable.
        {
            //then prompt the user to input three floating- point numbers and print the sum and average, of three numbers.
            // You will need at least 3 float or double variables. You can make additional variables to hold the sum and average if you wish, but that isn't necessary (you can just write the expression result directly to output). You'll also need output statements to inform the user about the result of the computations.
        }
        else if (the user enters b)// testing the value of the choice variable.
        {
            //then prompt the user to enter three initials of first name, middle name, and last name Print the initials.
            // You will need at least 3 variables to hold the inputs, and then an output statement to write them back out again.
        }
        // optionally, you can have an else block which prints an error message if the user did not input either 'a' or 'b' but something else instead.
    
        return 0;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center