CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Help With Declaring Variables

  1. #1
    Join Date
    Jan 2020

    Help With Declaring Variables

    Hi guys, I am working on this assignment for class. However, I am at declaring variables. Any help would be appreciated.

    I was given the input and asked to test the output.

    Tom 168.5 46 - How should I declare this
    and the variable declaration: (2,8) - How should I declare this

    double dec = 2.7;
    int num = 45;
    string str = "**";

    What I have right now:

    #include <iostream> 
    using namespace std; 
    int main ()
        cout << "This program was written by: Phil (Xinpeng) Tang" << endl;
        double dec = 2.7;
        int num = 45;
        string str = "**";
        cin >> str >> dec >> num;
        cout << str << " " << dec << " " << num << endl;
        return 0;
    Last edited by 2kaud; January 27th, 2020 at 04:20 AM. Reason: Added code tags

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012

    Re: Help With Declaring Variables

    When posting code, you should use code tags to format the code. Go Advanced, select the formatted code and click '#'.

    To use class string in a program, you need to have #include <string>

    These definitions mean that dec is of type double and has an initial value of 2.7 num is of type int and has a value of 45 and str is a string with a value of **

    The cin statement will obtain input from the console and assign input to str, dec and num.

    However, when obtaining string input using >>, note that string input is obtained up to the first white-space character (newline, tab, space). So

    string 34.5 22

    is a valid input (as input for str terminates on the space), but

    string23.5 33

    is not as the value string23.5 is assigned to str and 33 is assigned to dec and the system waits for input for num.

    Also note, that with modern C++ syntax, initialisation of a variable is now done using 'universal initialisation'. This form is:

    double dec {2.7};
    int num {45};
    string str {"**"};
    Last edited by 2kaud; January 27th, 2020 at 04:32 AM.
    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++17 Compiler: Microsoft VS2019 (16.4.5)

Posting Permissions

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)