CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jun 2017
    Location
    Berkeley CA
    Posts
    19

    Question regarding assigned values for c++ program

    Hello all,
    This is my first post.
    I am taking an online summer beginning c++ course.
    We are using C++ programming from problem analysis to program design 7th edition by malik.
    I understand some basic things with c++ such as how to write a program that prompts users to enter test score data and obtain the average of these test scores. I have learned about different data types, assignment statements, how to assign variables etc. some pretty basic things.
    I just learned about the get, putback, peek etc functions.
    There is an exercise at the end of chapter 3 (which I just read) that I do not understand what it is asking and I am hoping someone can shed some light on it or at least have a discussion with me and point me in the right direction.

    Heres the assignment:
    Suppose num1 and num2 are int variables and symbol is a char variable. Consider the following input: (2)
    47 18 * 28 $
    what value (if any) is assigned to num1,num2 and symbol after each of the following statements executes? (use the same input for each statement.)
    a. cin>>num1>>symbol>>num2;
    b. cin>>symbol>>num1>>num2;
    c. cin>>num1;
    cin.get(symbol);
    cin>>num2;
    d. cin>>num1>>num2;
    cin.get(symbol);
    e. cin.get(symbol);
    cin>>num1>>num2;


    That is the assignment
    What I get out of what it is asking:
    First of all I understand that the input is 5 things, some are int values and some are symbols (the * and $)
    for a: so num1's value would be 47 and then the program has an input failure because the next thing that is entered is not a symbol? is this the correct way to think about it?
    for b: the program fails right off the bat because the first thing we input is not a symbol it is a number?
    for c: num1=47
    symbol= (blank)
    num2=18?
    really not sure about this one..
    for d: num1=47
    num2=18
    symbol=*
    for e: program fails because first character the 4 is not a symbol?

    anyway if someone could steer me in the right direction that would be great.
    Thanks and let me know if the formatting of my question is correct or if i could write more/less to make it clear since I know nothing about programming language as I am just starting this course.

    Cheers,

    William

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

    Re: Question regarding assigned values for c++ program

    a: so num1's value would be 47 and then the program has an input failure because the next thing that is entered is not a symbol? is this the correct way to think about it?
    num1 is 47 but the next input is 1 which would be read to symbol as 1 is a char. Then what happens?

    for b: the program fails right off the bat because the first thing we input is not a symbol it is a number?
    No - for the same reasoning as a).

    for c: num1=47 symbol= (blank) num2=18?
    Spot on! >>num1 extracts the first 47 to num1 then .get() reads the following space then >>num2 extracts the 18.

    for d: num1=47 num2=18 symbol=*
    num1 is 47, num2 is 18 but symbol is <space> as the .get() gets the character following the last extraction which was 18 and so gets a <space>

    for e: program fails because first character the 4 is not a symbol?
    No, because for a) the initial 4 is a character which will be read into symbol.
    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)

  3. #3
    Join Date
    Jun 2017
    Location
    Berkeley CA
    Posts
    19

    Re: Question regarding assigned values for c++ program

    I believe I see what you are saying, will work on it and get back to you with my results
    Thank you very much

  4. #4
    Join Date
    Jun 2017
    Location
    Berkeley CA
    Posts
    19

    Re: Question regarding assigned values for c++ program

    Ok so here is what I have concluded
    for the same inputs like stated earlier which are:
    47 18 * 28 $
    A) cin>>num1>>symbol>>num2;

    num1=47
    symbol=1
    num2=8
    B) cin>>symbol>>num1>>num2;

    symbol=4
    num1=7
    num2=18
    C) cin>>num1;
    cin.get(symbol);
    cin>>num2;

    num1=47
    symbol= ' '
    num2=18
    D) cin>>num1>>num2;
    cin.get(symbol);

    num1=47
    num2=18
    symbol= ' '
    E) cin.get(symbol);
    cin>>num1>>num2;

    symbol=4
    num1=7
    num2-18

    How does this look?

    Thanks

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

    Re: Question regarding assigned values for c++ program

    a)

    b)

    c)

    d)

    e)

    Congrats!
    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)

  6. #6
    Join Date
    Jun 2017
    Location
    Berkeley CA
    Posts
    19

    Re: Question regarding assigned values for c++ program

    Thank you very much!
    very helpful!!!

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

    Re: Question regarding assigned values for c++ program

    However, usually in programming it is the other way round. Given an input format how would you read it into a program?

    Say the input was
    1) 12 34 +
    2) 12+i1
    3) +2+4+5
    4) 34 56 23 * + 11

    What statements/definitions would be needed to read these into a program?
    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)

  8. #8
    Join Date
    Jun 2017
    Location
    Berkeley CA
    Posts
    19

    Re: Question regarding assigned values for c++ program

    Here is what I think:
    int num;
    char symbol;

    1) cin>>num>>num>>symbol;
    2) cin>>num;
    cin.get(symbol);
    cin.get(symbol);
    cin.get(num);
    3) cin.get(symbol);
    cin.get(num);
    cin.get(symbol);
    cin.get(num);
    cin.get(symbol);
    cin.get(num);
    4) cin>>num>>num>>num>>symbol>>symbol>>num;

    My initial thought was to define 9 numbers: num1=12, num2=34, num3=1, num4=2, num5=4, num6=5, num7=56, num8=23, num9=11 The reason i thought to do this is because when i looked through what the input you gave me I saw 9 different numbers.
    In addition to this I saw 3 different symbols: symbol1=+, symbol2=i, symbol3=*.
    So 1 could be:
    cin>>num1>>num2>>symbol1;

    Which way would be correct? Is it necessary to assign all different numbers/symbols to different locations (i.e. num1, num2,...etc.) or is it sufficient to assign num as an int data type and leave it at that?

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

    Re: Question regarding assigned values for c++ program

    1)

    2) cin.get(num);

    .get() can only be used to extract character(s). To extract a number use >>

    3) Same as for 2)

    4)

    So 1 could be:
    cin>>num1>>num2>>symbol1;
    This would usually be what would be used so that the contents of both num1 and num2 are available in the program. If
    Code:
    cin >>num>>num>>symbol;
    is used, then num would be 34 and the 12 extracted from the stream would be 'lost'.

    Do you have access to a c++ compiler so that you can try these in actual 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)

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

    Re: Question regarding assigned values for c++ program

    There are lots of online console-based compilers available on the internet.
    One of them is www.cpp.sh, you don't even need to dig into the procedures of the installation or such.

  11. #11
    Join Date
    Jun 2017
    Location
    Berkeley CA
    Posts
    19

    Re: Question regarding assigned values for c++ program

    good afternoon!
    Yes I have x-code installed on my computer and have been trying things out

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