#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    80
    Rep Power
    1

    [Dev C++] First Learn to C++, Simple Calculator, Error Found!


    Hey C Programmers....

    I get some basics of python learned, but now i am try the basic of C++ yet...
    I get an simple calculator writted in 'dev c++' with called functions (void)....

    But in my script is just an error found..., this is my script:
    Code:
    // SIMPLE CALCULATOR, Dev C++
    #include <iostream>
    using namespace std;
    
    // Declarate all functions (or voids) before use it
    int calc_plus();
    int calc_min();
    int calc_devis();
    
    
    int main()
    {
    	string choosen;
    	
    	cout << "CALCULATOR: " << endl;
    	cout << "" << endl;
    	
    	cout << "Choose between '+', '-' or '/' and press 'Enter'";
    	cin >> choosen;
    	
    	if (choosen == "+")
    	{
    		calc_plus();
    	}
    	else if (choosen == "-")
    	{
    		calc_min();
    	}
    	else if (choosen == "/")
    	{
    		calc_devis();
    	}
    	else
    	{
    		cout << "Are not supported!";
    	}
    	
    	return choosen;
    }
    
    // Define Plus Calculator
    void calc_plus(int a, int b, int calc)
    {
    	cout << "First Number: ";
    	cin >> a;
    	
    	cout << "Second Number: ";
    	cin >> b;
    	
    	calc = a + b;
    	cout << "Total: " << calc;
    	
    	return a, b, calc;
    }
    
    // Define Min Calculator
    void calc_min(int a, int b, int calc)
    {
    	
    	cout << "First Number: ";
    	cin >> a;
    	
    	cout << "Second Number: ";
    	cin >> b;
    	
    	calc = a - b;
    	cout << "Total: " << calc;
    	
    	return a, b, calc;
    }
    
    // Define Devision Calculator
    void calc_devis(int a, int b, int calc)
    {
    	
    	cout << "First Number: ";
    	cin >> a;
    	
    	cout << "Second Number: ";
    	cin >> b;
    	
    	calc = a / b;
    	cout << "Total: " << calc;
    	
    	return a, b, calc;
    }
    This just an simple calculator to learn the basic of it....

    So this is my error in the Dev C++ Compiler:
    Compiling single file...
    --------
    - Filename: C:\Documents and Settings\Eigenaar\Bureaublad\jamie\Script\Script CPP\Variables\CPP Variable10 - Calculator.cpp
    - Compiler Name: TDM-GCC 4.9.2 32-bit Release

    Processing C++ source file...
    --------
    - C++ Compiler: C:\Program Files\Dev-Cpp\MinGW64\bin\g++.exe
    - Command: g++.exe "C:\Documents and Settings\Eigenaar\Bureaublad\jamie\Script\Script CPP\Variables\CPP Variable10 - Calculator.cpp" -o "C:\Documents and Settings\Eigenaar\Bureaublad\jamie\Script\Script CPP\Variables\CPP Variable10 - Calculator.exe" -m32 -I"C:\Program Files\Dev-Cpp\MinGW64\include" -I"C:\Program Files\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include" -I"C:\Program Files\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include" -I"C:\Program Files\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++" -L"C:\Program Files\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib32" -static-libgcc -m32
    C:\Documents and Settings\Eigenaar\Bureaublad\jamie\Script\Script CPP\Variables\CPP Variable10 - Calculator.cpp: In function 'int main()':
    C:\Documents and Settings\Eigenaar\Bureaublad\jamie\Script\Script CPP\Variables\CPP Variable10 - Calculator.cpp:38:9: error: cannot convert 'std::string {aka std::basic_string<char>}' to 'int' in return
    return choosen;
    ^

    C:\Documents and Settings\Eigenaar\Bureaublad\jamie\Script\Script CPP\Variables\CPP Variable10 - Calculator.cpp: In function 'void calc_plus(int, int, int)':
    C:\Documents and Settings\Eigenaar\Bureaublad\jamie\Script\Script CPP\Variables\CPP Variable10 - Calculator.cpp:53:15: error: return-statement with a value, in function returning 'void' [-fpermissive]
    return a, b, calc;
    ^

    C:\Documents and Settings\Eigenaar\Bureaublad\jamie\Script\Script CPP\Variables\CPP Variable10 - Calculator.cpp: In function 'void calc_min(int, int, int)':
    C:\Documents and Settings\Eigenaar\Bureaublad\jamie\Script\Script CPP\Variables\CPP Variable10 - Calculator.cpp:69:15: error: return-statement with a value, in function returning 'void' [-fpermissive]
    return a, b, calc;
    ^

    C:\Documents and Settings\Eigenaar\Bureaublad\jamie\Script\Script CPP\Variables\CPP Variable10 - Calculator.cpp: In function 'void calc_devis(int, int, int)':
    C:\Documents and Settings\Eigenaar\Bureaublad\jamie\Script\Script CPP\Variables\CPP Variable10 - Calculator.cpp:85:15: error: return-statement with a value, in function returning 'void' [-fpermissive]
    return a, b, calc;
    ^
    I am an pure newbie in C++ and later i want maby use Dev C++ for 3D(Printing) Software using OpenGL...,
    but now i try learn the basic of it...

    Can anyone correct my code pleace, just i can hopefully learn about this ?....
    Thnks for help, Jamie.
  2. #2
  3. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,743
    Rep Power
    9646
    It seems like you're trying to take your Python experience and cram it into C++. That isn't going to work.

    Set aside everything you know about Python and start from scratch. Learn about variable types. Learn about functions and return values.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    80
    Rep Power
    1
    Originally Posted by requinix
    It seems like you're trying to take your Python experience and cram it into C++. That isn't going to work.

    Set aside everything you know about Python and start from scratch. Learn about variable types. Learn about functions and return values.
    Yes.... but als I get downloading free apps called SoloLearn and now I am
    by lessen 2: conditionals and loops...

    So I am know About strings and int numbers and I am New in C++ yet,
    Just I must learn much more about C...

    I am planned to go further to evening after eating!...

    Just I am try to learn from scrach on my mobile app I download to...
  6. #4
  7. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,743
    Rep Power
    9646
    Well, either the app hasn't taught you stuff like C++ not being able to magically convert strings to integers, that a return type of "void" means the function does not return anything at all, or that you can't return multiple values from a function, or you haven't gotten to the point in the app where it does tell you about all that.
    If it's the former then the app isn't very good, but if it's the latter then you're getting too far ahead of it and need to slow down and follow what it teaches.
  8. #5
  9. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,743
    Rep Power
    9646
    So apparently I'm not in a good mood? I'll elaborate on what I just said.

    Code:
    C:\Documents and Settings\Eigenaar\Bureaublad\jamie\Script\Script CPP\Variables\CPP Variable10 - Calculator.cpp: In function 'int main()':
    C:\Documents and Settings\Eigenaar\Bureaublad\jamie\Script\Script CPP\Variables\CPP Variable10 - Calculator.cpp:38:9: error: cannot convert 'std::string {aka std::basic_string<char>}' to 'int' in return
      return choosen;
             ^
    choosen (which should be "chosen", by the way) is a string variable. You can't use a string where C++ expects an int. And no, you cannot change main() to return a string because main() is special and needs to return specifically an int.
    The value it returns is traditionally zero for success and non-zero for failure, so your code should simply be returning 0.

    Code:
    C:\Documents and Settings\Eigenaar\Bureaublad\jamie\Script\Script CPP\Variables\CPP Variable10 - Calculator.cpp: In function 'void calc_plus(int, int, int)':
    C:\Documents and Settings\Eigenaar\Bureaublad\jamie\Script\Script CPP\Variables\CPP Variable10 - Calculator.cpp:53:15: error: return-statement with a value, in function returning 'void' [-fpermissive]
      return a, b, calc;
                   ^
    The error specifically means that you're trying to return a value from a function that is not supposed to return a value. Additionally I, as a human being, know you're trying to return multiple values, however you're accidentally using a syntax that happens to be valid so the compiler does not know. So there's two problems in there.
    The good news is that those functions shouldn't return anything. As in there is no reason for them to do so. At all. Remove the entire return statement from all three functions.


    That's the compiler messages but there's still more wrong about the code.

    Your function declarations do not match the actual implementations. Maybe you changed your mind partway through the implementation? I really hope this app thing you're using did not tell you to do that.
    The functions should not have a return value (it doesn't make sense) and they don't take any parameters (nothing to pass to them). Fix your declarations and implementations to match that. Note that removing the function parameters will force you to use regular variables inside each function - another thing the app should have told you about by now.

    The other one is more fun. Try using your calculator to do division. Say, 10 / 3. What do you expect it to output and what does it actually output?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    80
    Rep Power
    1
    Originally Posted by requinix
    So apparently I'm not in a good mood? I'll elaborate on what I just said.

    Code:
    C:\Documents and Settings\Eigenaar\Bureaublad\jamie\Script\Script CPP\Variables\CPP Variable10 - Calculator.cpp: In function 'int main()':
    C:\Documents and Settings\Eigenaar\Bureaublad\jamie\Script\Script CPP\Variables\CPP Variable10 - Calculator.cpp:38:9: error: cannot convert 'std::string {aka std::basic_string<char>}' to 'int' in return
      return choosen;
             ^
    choosen (which should be "chosen", by the way) is a string variable. You can't use a string where C++ expects an int. And no, you cannot change main() to return a string because main() is special and needs to return specifically an int.
    The value it returns is traditionally zero for success and non-zero for failure, so your code should simply be returning 0.

    Code:
    C:\Documents and Settings\Eigenaar\Bureaublad\jamie\Script\Script CPP\Variables\CPP Variable10 - Calculator.cpp: In function 'void calc_plus(int, int, int)':
    C:\Documents and Settings\Eigenaar\Bureaublad\jamie\Script\Script CPP\Variables\CPP Variable10 - Calculator.cpp:53:15: error: return-statement with a value, in function returning 'void' [-fpermissive]
      return a, b, calc;
                   ^
    The error specifically means that you're trying to return a value from a function that is not supposed to return a value. Additionally I, as a human being, know you're trying to return multiple values, however you're accidentally using a syntax that happens to be valid so the compiler does not know. So there's two problems in there.
    The good news is that those functions shouldn't return anything. As in there is no reason for them to do so. At all. Remove the entire return statement from all three functions.


    That's the compiler messages but there's still more wrong about the code.

    Your function declarations do not match the actual implementations. Maybe you changed your mind partway through the implementation? I really hope this app thing you're using did not tell you to do that.
    The functions should not have a return value (it doesn't make sense) and they don't take any parameters (nothing to pass to them). Fix your declarations and implementations to match that. Note that removing the function parameters will force you to use regular variables inside each function - another thing the app should have told you about by now.

    The other one is more fun. Try using your calculator to do division. Say, 10 / 3. What do you expect it to output and what does it actually output?
    OK then.... maby i get this SoloLearn / Learn C++ App Cursus all ready finnished, i plans to look to youtube tutorials, example scripts
    and even i plans to paid books (first plans = C++ for Dummies / Translated=Dutch) and other C++ Books yet...

    I think thad only the cursus (cursus from=SoloLearn) is not all basic i must be learn to get correctly knowledge in C...
    In python i get it learned from "CodeAccademy", Python for Dummies (Translated=Dutch)) and i get even reseverd
    one other book of python... even an python handbook to learn later....

    Just i plans do the same way to learn the basics of C(++) yet.... i get more plans to try learn many basics of C...
    Now i get further to learn C++ / Conditionals and Loops...

    Jamie...
  12. #7
  13. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,967
    Rep Power
    509
    I'll give a bit of detail about requinix's delightful comments:
    Code:
    // Define Plus Calculator
    //int calc_plus();                        // declaration near start of program
     void calc_plus(int a, int b, int calc)   // definition mismatch
    {
            //...
            // yet more mismatch.
    	return a, b, calc;
    }
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    80
    Rep Power
    1
    OK...., i get here an script, just call an while loop with called as an input...
    This is as example what i get learned yesterday, i get even learned to work
    with cases (cases works only with numbers yet):

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int number_min;
    	int number_reapet;
    	int number_step;
    	
    	cout << "Number Min: ";
    	cin >> number_min;
    	
    	cout << "Number Reapet: ";
    	cin >> number_reapet;
    	
    	cout << "Number Step: ";
    	cin >> number_step;
    	
    	while (number_min < number_reapet)
    	{
    		cout << "Reapet = " << number_min << endl;
    		number_min = number_min + number_step;
    	}
    	
    	return 0;
    }
    I am learn C++ just from scrach using to begin in C...., the SoloLearn app...., i am now by lession 3 (arrays, pointners, etc) and i plans to go
    tomorrow further with C..... to evening i get swimming yet...

    If i am already finnished with the SoloLearn app... to learn some basics of C++, i plans to buy later books for C++, called for Dummies i think...
    But first i get reserved an book for python as example...

    I plans too, to follow C++ tutorials on youtube yet....





    Edit:

    Oh yeah...., i can also declarate all in one int statement, like this:
    Code:
    int number_min, number_reapet, number_step;


    So... and this is what i learned about cases:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int num;
    	
    	cout << "Typ an Number: ";
    	cin >> num;
    	
    	switch (num)
    	{
    		case 0:
    			cout << "Number is Zero: " << num << endl;
    			break;
    		case 1:
    			cout << "Number is One: " << num << endl;
    			break;
    		case 2:
    			cout << "Number is Two: " << num << endl;
    			break;
    		case 3:
    			cout << "Number is Three: " << num << endl;
    			break;
    		case 4:
    			cout << "Number is Four: " << num << endl;
    		default:
    			cout << num << " is the default number!" << endl;
    	}
    	
    	return 0;
    }

    And this is as example what i nearned about "if statements and operators"....
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int value;
    	
    	cout << "Typ an value between the 0 and the 10: ";
    	cin >> value;
    	
    	if (value > 0 && value < 10)
    	{
    		cout << "Well Done!, Value is between 0 and 10, excacly typed to " << value << endl;
    	}
    	else if (!(value > 0))
    	{
    		cout << "You Lose!, Value is under the 0" << endl;
    	}
    	else if (!(value < 10))
    	{
    		cout << "You Lose!, Value is above the 10" << endl;
    	}
    	
    	return 0;
    }

    But thanks for help, Jamie....
    Last edited by JamieVanCadsand; May 1st, 2018 at 04:34 AM.
  16. #9
  17. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,967
    Rep Power
    509
    By all the gods you're ready to program the Mars invasion. Seriously, though, returning an int from main is better success than many coders presenting at this forum.
    [code]Code tags[/code] are essential for python code and Makefiles!
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    80
    Rep Power
    1
    Originally Posted by b49P23TIvg
    By all the gods you're ready to program the Mars invasion. Seriously, though, returning an int from main is better success than many coders presenting at this forum.
    OK...... how do you means ?....



    But also..... if i work with cases, this is my way to create (for beginning) an simple calculator, only without exceptions, when for exeptions i must
    learn more about C++ yet....


    Beginning Calculator, written in "Dev C++" on an older computer, called at Windows XP:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int choosen;
    	
    	cout << "CALCULATOR PROGRAM" << endl;
    	
    	cout << "Chooses: 1 - By / 2 - Mines / 3 - Multiply / 4 - Devision" << endl;
    	cout << "Your choose: ";
    	cin >> choosen;
    	
    	switch (choosen)
    	{
    		case 1:
    			int by1, by2, by_calc;
    			
    			cout << endl;
    			cout << "By Number1: ";
    			cin >> by1;
    			
    			cout << "By Number2: ";
    			cin >> by2;
    			
    			cout << endl;
    			
    			by_calc = by1 + by2;
    			
    			cout << "Total: " << by_calc << endl;
    			break;
    			
    		case 2:
    			int min1, min2, min_calc;
    			
    			cout << endl;
    			cout << "Mines Number1: ";
    			cin >> min1;
    			
    			cout << "Mines Number2: ";
    			cin >> min2;
    			
    			cout << endl;
    			
    			min_calc = min1 - min2;
    			
    			cout << "Total: " << min_calc << endl;
    			break;
    			
    		case 3:
    			int mult1, mult2, mult_calc;
    			
    			cout << endl;
    			cout << "Multi Number1: ";
    			cin >> mult1;
    			
    			cout << "Multi Number2: ";
    			cin >> mult2;
    			
    			cout << endl;
    			
    			mult_calc = mult1 * mult2;
    			
    			cout << "Total: " << mult_calc << endl;
    			break;
    			
    		case 4:
    			int dev1, dev2, dev_calc;
    			
    			cout << endl;
    			cout << "Devision Number1: ";
    			cin >> dev1;
    			
    			cout << "Devision Number2: ";
    			cin >> dev2;
    			
    			cout << endl;
    			
    			dev_calc = dev1 / dev2;
    			
    			cout << "Total: " << dev_calc << endl;
    			break;
    	}
    	
    	return 0;
    }
    Run this script, you will see it works!...


    I plans if i get time to evening, to learn further about C++, now i am by lession 3 > Data Types, Arrays and Pointners..... yesterday i don't get
    time when i must swimming..... but i hope i get to evening time...

    Jamie...
  20. #11
  21. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,967
    Rep Power
    509
    You're certain the program works? I divided 64 by 65, which is nearly 1, and the program says it's 0.
    Code:
    $ ./C
    CALCULATOR PROGRAM
    Chooses: 1 - By / 2 - Mines / 3 - Multiply / 4 - Devision
    Your choose: 4
    
    Devision Number1: 64
    Devision Number2: 65
    
    Total: 0
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo