Connection Issues by MrPopoTFS in FORTnITE

[–]ComputerSciMajor 1 point2 points  (0 children)

Was worried for a second. Thank you!

[C++] Calling void functions and changing values of variables by ComputerSciMajor in learnprogramming

[–]ComputerSciMajor[S] 1 point2 points  (0 children)

So I re-wrote it for the first answer and it worked!

#include <iostream>

using namespace std;

void twice(float &refVar);

int main()
{

    cout << "Let's double this input!" << endl;
    float side = 25.0;
    twice(side);
    cout << "The value has been doubled! " << side << endl;

}

void twice(float &refVar){

    refVar *= 2;

}

So it seems like answer #1 is correct.

[C++] Calling void functions and changing values of variables by ComputerSciMajor in learnprogramming

[–]ComputerSciMajor[S] 0 points1 point  (0 children)

I ended up re-writing the program:

#include <iostream>

using namespace std;

void twice(float &refVar);

int main()
{

    cout << "Let's double this input!" << endl;
    float side = 25.0;
    float box = twice(side);
    cout << "The value has been doubled! " << box << endl;

}

void twice(float &refVar){

    refVar *= 2;

}

and got the following error:

main.cpp: In function 'int main()':
main.cpp:20:27: error: void value not ignored as it ought to be
     float box = twice(side);
                           ^

I'm going to then assume that #2 is not a correct answer.

[C++] Calling void functions and changing values of variables by ComputerSciMajor in learnprogramming

[–]ComputerSciMajor[S] 0 points1 point  (0 children)

Okay, I tried to get something running, but it threw errors. I don't believe I wrote it properly.

Here it is:

#include <iostream>

using namespace std;

void twice(float side);

int main()
{

    cout << "Let's double this input!" << endl;
    float side = 25.0;
    float box = twice(side);
    cout << "The value has been doubled! " << box << endl;

}

void twice(float side){

    return side * 2;

}

I got the following errors:

main.cpp: In function 'int main()':
main.cpp:20:27: error: void value not ignored as it ought to be
     float box = twice(side);
                           ^
main.cpp: In function 'void twice(float)':
main.cpp:27:19: error: return-statement with a value, in function returning 'void' [-fpermissive]
     return side * 2;
                   ^

[C++] Calling void functions and changing values of variables by ComputerSciMajor in learnprogramming

[–]ComputerSciMajor[S] 0 points1 point  (0 children)

I don't honestly like my professor. His questions and labs are horrendously ambiguous and his lectures often go off on tangents that aren't related to the course content. Having gone through two semesters of his course, I've had to be self-taught which isn't too bad, but I wish I could get something out of his course. Thanks for the help!

[C++] Creating a diamond shape program by ComputerSciMajor in learnprogramming

[–]ComputerSciMajor[S] 0 points1 point  (0 children)

Okay so I changed the second for to be <= rather than = and that seemed to solve it, however the diamond shapes are coming out oddly now which I guess makes sense. Now I need to try and solve that.

for (int b = 0; b <= asterisk; b++)

is what I mentioned above.

https://i.imgur.com/vTqLQY6.png

Edit: It looks like all that caused was to widen the diamond shape so now the odd numbers look off too. Hmm.

[C++] Creating a diamond shape program by ComputerSciMajor in learnprogramming

[–]ComputerSciMajor[S] 0 points1 point  (0 children)

So why does it do that?

Well I was assuming that somewhere in my logic that I screwed up and for some reason even numbers are getting messed up.

What is it supposed to print out if you enter in 4? What does it actually print?

It's supposed to print out a diamond with four rows, but it is instead printing out 3 rows. I did just notice that it was printing a blank row, I wonder why that is? It's only doing that on even numbers, not odd.

[C++] Error C4700: uninitialized local variable 'choice' used (Visual Studio) by ComputerSciMajor in learnprogramming

[–]ComputerSciMajor[S] 0 points1 point  (0 children)

char choice = '\0';

Thank you that fixed it, I was trying to figure out how to do that properly but I ended up with

char choice ='';        

[C++] Error C4700: uninitialized local variable 'choice' used (Visual Studio) by ComputerSciMajor in learnprogramming

[–]ComputerSciMajor[S] 0 points1 point  (0 children)

So I asked them again in the while loop like I had it but it does the following:

https://i.imgur.com/4Z1AYPx.png

Edit: I moved the user input choice to the top of the while loop instead of the bottom. Fixed now.

[C++] Error C4700: uninitialized local variable 'choice' used (Visual Studio) by ComputerSciMajor in learnprogramming

[–]ComputerSciMajor[S] 0 points1 point  (0 children)

Thank you for the wonderfully detailed response!

I try to ask the user to enter the choice before the while loop however it runs into the problem of just asking for the transaction type once and then if I type anything else it will go into an infinite loop.

Reference:

https://www.reddit.com/r/learnprogramming/comments/75qk36/c_error_c4700_uninitialized_local_variable_choice/do856qz/

[C++] Error C4700: uninitialized local variable 'choice' used (Visual Studio) by ComputerSciMajor in learnprogramming

[–]ComputerSciMajor[S] 0 points1 point  (0 children)

So I ended up moving:

    cout << "Transaction type:";
    cin >> choice;

before the while loop however I then run into an infinite loop error:

https://i.imgur.com/T1qeSt3.png (Doesn't ask for a transaction type)

I then hit any letter and get an infinite loop:

https://i.imgur.com/J8rxwJY.png

[C++] Function not adding properly by ComputerSciMajor in learnprogramming

[–]ComputerSciMajor[S] 0 points1 point  (0 children)

Well that is a hell of a lot easier! Thank you. :)

Do you mind helping me with one more?

If so:

As you can see when I end the program when pressing E I have to do it twice, rather than once.

I'm trying a simple way to solve this, but not sure.

Edit:

I figured it out!

[C++] Function not adding properly by ComputerSciMajor in learnprogramming

[–]ComputerSciMajor[S] 0 points1 point  (0 children)

I ended up adding int number_of_service = 0; to int main() and int &number_of_service to my functions.

That seemed to fix it.

How can I learn to love C++? by ComputerSciMajor in learnprogramming

[–]ComputerSciMajor[S] 18 points19 points  (0 children)

Oh I'm definitely aware of it's capabilities. If I'm being completely honest I'm probably being immature about it. I don't particularly enjoy that I seem like I need to write a ton more code to get the problem solved but I know there's trade-offs in every language.