CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2017
    Posts
    1

    How would I write this in C++?

    I'm currently in intro to programming and I am very confused as how to write this in C++, I would appreciate an explanation too if possible:

    1. Ask the user for an integer. Make sure all variables are appropriately initialized to zeros, i.e., integer variables should be equal to 0 and floating-point variables should be equal to 0.0.
    2. Get the integer and store it into a variable.
    3. Ask the user for a floating-point number.
    4. Get the number and store it into a variable.
    5. Display a blank line, i.e., separate the output from the input.
    6. On its own line, display a message along with the integer on the screen.
    7. On its own line, display a message along with the number to three decimal places on the screen.
    8. Display a blank line.
    9. On its own line, display a message with your name in it.

    This is what I have so far, but I don't know what to do from here:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
        printf("Enter an integer: \n");
        system("pause");
    }
    Last edited by 2kaud; May 28th, 2017 at 03:55 PM.

  2. #2
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    720

    Re: How would I write this in C++?

    Hi, it took 5 minutes for me to write this simple program.
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    
    int main()
    {
    	int iVar = 0;
    	double dVar = 0.0;
    
    	//input the values
    	cout << "Enter an integer number: ";
    	cin >> iVar;
    	cout << "Enter a floating-point number: ";
    	cin >> dVar;
    	cout << endl;
    
    	//outputing the variables
    	cout << "The entered integer number is: " << iVar << endl;
    	cout << "The entered floating-point number is: " << fixed << setprecision(3) << dVar << endl << endl;
    	cout << "My name is Mr. Burdon";
        return EXIT_SUCCESS;
    }
    Have a nice day.
    Last edited by AvDav; May 28th, 2017 at 02:09 PM.

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: How would I write this in C++?

    Quote Originally Posted by bbudron View Post
    I'm currently in intro to programming and I am very confused as how to write this in C++, I would appreciate an explanation too if possible:
    ...
    If it is a homework then have a look at this FAQ
    Victor Nijegorodov

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: How would I write this in C++?

    How would I write this in C++
    The code as posted in post #1 is more c than c++. The code as posted by AvDav in post #2 would be the expected c++ code.

    How are you learning c++ - from a book, teacher or ? If you are learning c++ then you are not being taught to code c++ as expected. Whilst most c code will compile for c++, there are ways of doing things the c++ way which are often different to the c way and it is the c++ ways which should be learnt and used.

    Also note the link in Victor's post #3. If this is a homework exercise then expecting forum members to write it is a real no-no and would be considered cheating. In these cases we could provide advice and guidance as to how to how to proceed rather than providing the code.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Feb 2017
    Posts
    677

    Re: How would I write this in C++?

    Quote Originally Posted by bbudron View Post
    I'm currently in intro to programming and I am very confused as how to write this in C++, I would appreciate an explanation too if possible:
    When taking the first practical steps into a language a simple tutorial often is of great help. There are several on the internet like this one for example,

    http://www.cplusplus.com/doc/tutorial/

    Under "Basic Input/Output" it is explained how you do simple input and output using the standard IO streams (cin and cout).

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: How would I write this in C++?

    My suggestion for an internet tutorial is http://www.learncpp.com/ which has nearly completed a recent thorough overhaul.

    If you are learning from a book, I would suggest nothing published prior to 2015 so that it covers c++14. There are many, many second hand c++ books available extremely cheaply but I would advise that you stear clear of those published prior to 2015 and especially those published prior to 2012 that don't cover c++11. c++11 (the c++ standard released in 2011) was a major upgrade from the previous standard (c++98) and the way that c++ is now used changed following the release of that standard. The current standard is c++14 (released 2014) is the current c++ standard and the new c++17 standard is expected to be released shortly. Also, the way in which c++ is used/taught has changed over the years so that it is important that the current 'modern' c++ approach is learnt.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Feb 2017
    Posts
    677

    Re: How would I write this in C++?

    Quote Originally Posted by AvDav View Post
    Hi, it took 5 minutes for me to write this simple program.
    A famous painter was approached in a restaurant and asked if he please would draw a small sketch on a napkin. The painter did that and handed it over. The person then insisted she pay for it and asked how much she owed. It's a gift the painter replied but if you want to pay it will be 50.000 Franc. Om my god she exclaimed but it took just a few minutes. No the painter said, it took a lifetime.

    If you think about it, I'm sure it took you more than five minutes to write that code.

    Talk is cheap, show me the code.
    This must be an abbreviation. If the quote is by Linus Torvalds it more likely would go like this,

    "Talk is cheap, show me the code, unless it's C++ then shove it"

    This is his (in)famous C++ rant,

    https://lwn.net/Articles/249460/

    As you can see, Linus Torvalds may not be the most suitable celebrity to name-drop at a C++ forum.
    Last edited by wolle; May 30th, 2017 at 09:24 AM.

  8. #8
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    720

    Re: How would I write this in C++?

    Well, actually I have trained my brain in such a way, that writing these kind of IO programs is a really child's play.
    And I don't think it could be some university homework, even if it is, good bless Armenian educational system, where I was busy developing graph traversal algorithms.
    Uhm, but this is really an off-topic.

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured