Multiple int main()'s? - c++

I just started learning c++ and im trying to expose myself to simple additions and subtractions. However I cant seem to get this function(s) running. Any input would be well appreciated. I also accept constructive criticism :)
#include "stdafx.h"
#include <iostream>
int main()
{
std::cout << " I have a qustion for you Devante. Here it is . . . if you add the word two to the number 2, what do you get ?";
int x = 4;
std::cin >> x;
std::cout << "Correct, the correct answer is " << x << std::endl;
return 0;
}
int main()
{
std::cout << " Since you got the answer right this time, lets see if you can subtract. What is 6 - 6 ? ";
int x = 0;
std::cin >> x;
std::cout << "Correct, the answer is " << x << std::endl;
return 0;
}

You can only have one main function in a program.
What you can do is...
Put the code in those functions in two differently named functions.
Call them from main.
int test1()
{
std::cout << " I have a qustion for you Devante. Here it is . . . if you add the word two to the number 2, what do you get ?";
int x = 4;
std::cin >> x;
std::cout << "Correct, the correct answer is " << x << std::endl;
return 0;
}
int test2()
{
std::cout << " Since you got the answer right this time, lets see if you can subtract. What is 6 - 6 ? ";
int x = 0;
std::cin >> x;
std::cout << "Correct, the answer is " << x << std::endl;
return 0;
}
int main()
{
test1();
test2();
return 0;
}

main represents the entry point of your program. You cannot have multiple.

You can't have more than one definition of any function.

Related

Compiler stating error 2059 for struct and int description

I'm (probably obviously) very new, and am attempting to build a calculator for my first project. I wanted to test my first concept, but upon compiling I get the 2059 error for the end brace of my InterFace struct as well as the first brace of my int AddUp. These seem like totally random errors. If it helps, the errors are for lines (10,1) and (16,2), although I suspect the 1 and 2 refer to number of like errors recorded? Any help would be appreciated.
1 #include <iostream>
2
3 struct InterFace
4 {
5 char Buttons[4][4]
6 {
7 Buttons[1] = "\u00B1";
8 std::cout << Buttons[1] << std::endl;
9 }
10 };
11
12
13 struct Addition
14 {
15 int AddUp[2]
16 {
17
18 }
19 };
int main()
{
std::cin.get();
}
You do not have the correct core concepts right, and should probably work through some C++ tutorials or courses before writing a program like this.
A few things:
The ± symbol is a unicode character. char in C++ refers to a single byte, usually an ASCII value if it's referring to text data. So it can't store the unicode +- symbol. Instead, you can store this unicode value in an std::string buttons[4][4]; (although the full answer is much more complicated).
In C++, 'a' refers to the character a, but "a" refers to a const char*. If it wasn't for the unicode issue, you should have used single quotes.
You try to assign to Buttons[1], but buttons is a 2-dimensional array. The element 1 also refers to the second element of the array, which may not be what you intended. Instead you could write Buttons[0][0]='a';
You don't have the concept of a member function/member variable down. The proper way to do this would be to have an initializer function and then call it.
Here is a fixed/working example, but I really recommend going through other tutorials first!
#include <iostream>
#include <string>
struct Interface {
std::string buttons[4][4];
void initialize_interface() {
buttons[0][0] = std::string("\u00B1");
std::cout << buttons[0][0] << std::endl;
}
};
int main() {
Interface my_interface;
my_interface.initialize_interface();
return 0;
}
As M.M. notes in the comments, a more paradigmatic approach would be the following:
#include
#include
struct Interface {
std::string buttons[4][4];
Interface() {
buttons[0][0] = std::string("\u00B1");
std::cout << buttons[0][0] << std::endl;
}
};
int main() {
Interface my_interface;
return 0;
}
Interface::Interface is called the constructor, and it runs upon initialization.
Since I wasn't able to build the calculator as I initially intended, I went a different route. I completed a basic one using switch instead.
#include <iostream>
int main()
{
int r;
int a;
int b;
int result1;
int result2;
int result3;
int result4;
int result5;
std::cout << "Please choose from the available options:" << std::endl << "0. Add" << std::endl << "1. Subtract" << std::endl << "2. Multiply" << std::endl << "3. Divide" << std::endl << "4. Modulo" << std::endl;
std::cin >> r;
switch (r % 5)
{
case 0:
std::cout << "You have chosen to Add, please enter two digits" << std::endl;
std::cin >> a;
std::cin >> b;
result1 = a + b;
std::cout << "Your sum is " << result1 << std::endl;
break;
case 1:
std::cout << "You have chosen to Subtract, please enter two digits" << std::endl;
std::cin >> a;
std::cin >> b;
result2 = a - b;
std::cout << "Your difference is " << result2 << std::endl;
break;
case 2:
std::cout << "You have chosen to Multiply, please enter two digits" << std::endl;
std::cin >> a;
std::cin >> b;
result3 = a * b;
std::cout << "Your product is " << result3 << std::endl;
break;
case 3:
std::cout << "You have chosen to Divide, please enter two digits" << std::endl;
std::cin >> a;
std::cin >> b;
result4 = a / b;
std::cout << "Your quotient is " << result4 << std::endl;
break;
case 4:
std::cout << "You have chosen to perform Modulus, please enter two digits" << std::endl;
std::cin >> a;
std::cin >> b;
result5 = a % b;
std::cout << "Your answer is " << result5 << std::endl;
break;
}
std::cin.get();
std::cin.get();
}

Test an integer value to determine if it is odd or even in C++

I have to write a program to test an integer value to determine if it is odd or even, and make sure my output is clear and complete. In other words, I have to write the output like "the value 4 is an even integer". I was also hinted that I have to check the value using the remainder modulo.
The issue I have is with the scanf() function. I get a syntax error:
'%=' expected a ')'
How do I fix this?
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
int main()
{
int number = 0;
cout << "enter an integer ";
int scanf(%=2 , &number);
if (number == 0)
cout << "the value" << number << "is even";
else
cout << "the value" << number << "is odd";
return 0;
}
You are using scanf() incorrectly (read the scanf() documentation on cppreference.com). The first parameter expects a null-terminated string containing the format to scan, but you are not passing in anything that even resembles a string. What you are passing in is not valid string syntax, per the C++ language standard. That is why you are getting a syntax error.
You need to change this line:
int scanf(%=2 , &number);
To this instead:
scanf("%d", &number);
Though, in C++ you really should be using std::cin instead for input (you are already using std::cout for output):
std::cin >> number;
Try this:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
int number = 0;
cout << "enter an integer ";
if (cin >> number)
{
if ((number % 2) == 0)
cout << "the value " << number << " is even";
else
cout << "the value " << number << " is odd";
}
else
cout << "the value is invalid";
return 0;
}
I know this question is a little dated, however, if you are able to use modern C++ features. You can write a constexpr helper function such as this:
#include <cstdint>
constexpr bool isEven(uint32_t value) {
return ((value%2) == 0);
}
Then in your main function, you can traverse through a loop of N integers and output your display such as:
#include <iostream>
#include <iomanip>
int main() {
for ( int i = 0; i < 100; i++ ) {
std::cout << std::setw(3) << std::setfill('0') << i << " is "
<< (isEven(i) ? "even" : "odd") << '\n';
}
return 0;
}
It's literally that simple. Here's another nice feature of using the constexpr helper function... You can also format your output as such:
int main() {
for ( int i = 0; i < 100; i++ ) {
std::cout << std::setw(3) << std::setfill('0') << i << ": "
<< std::boolalpha << isEven(i) << '\n';
}
return true;
}
If you are looking for something that is more efficient than using the modulo operator you can bitwise & with the least significant digit... The code above would then become:
#include <cstdint>
constexpr bool isOdd(uint32_t value) {
return (value&1);
}
And using it would be very similar as above, just make sure you reverse the wording in your output to match that from the function being used...
#include <iostream>
#include <iomanip>
int main() {
for ( int i = 0; i < 100; i++ ) {
std::cout << std::setw(3) << std::setfill('0') << i << " is "
<< (isOdd(i) ? "odd" : "even") << '\n';
}
return 0;
}
Again you can use the std::boolalpha manipulator to get this kind of output:
int main() {
for ( int i = 0; i < 100; i++ ) {
std::cout << std::setw(3) << std::setfill('0') << i << ": "
<< std::boolalpha << isOdd(i) << '\n';
}
return true;
}

Variable not changing from user input in C++

I'm trying to create multiple calculators in the C++ console for Geometry Theorems and other formulas in Algebra, and for some weird reason on the start of the program, when selecting an option the variable scene does not want to change(shown before the array of calculators[], and instead of going to the Pythagorean Theorem(scene 1), the console says, "Press any key to continue. . ." and closes.
I've tried both the switch() andif() statements to navigate scene management, but what am I doing incorrectly? (I'm still a C++ learner by the way, but I have other programming language experience).
Thanks for the help in advance.
#include "stdafx.h"
#include <iostream>
#include <cmath>
int scene(0);
char calculators[3][25] =
{
"",
"Pythagorean Theorem",
"Homer's Formula"
};
void selection()
{
std::cout << "Enter a number to select a calculator." << std::endl; // Opening
for (int i = 1; i <= 2; i += 1) {
std::cout << "Option " << i << ": " << calculators[i] << std::endl;
}
}
void pTheorem()
{
int a;
int b;
std::cout << "Enter side a: ";
std::cin >> a;
std::cout << "Enter side b: ";
std::cin >> b;
std::cout << "Side length of c is " << sqrt(pow(a, 2) + pow(b, 2)) << std::endl;
}
int main()
{
switch(scene)
{
case 0:
selection();
std::cin >> scene;
std::cout << "You've selected the " << calculators[scene] << " Calculator" << std::endl;
break;
case 1:
pTheorem();
break;
}
return 0;
}
Your main problem is that scene has been declared and initialized 0 at the beginning(globally) itself. This will give you always the same switch case = 0. Changing scene inside the switch cases will not work. Instead, you need to input the scene before the switch.
int main()
{
selection();
int scene = 0;
std::cin >> scene;
switch(scene)
{
......
}
}
Secondly, use std::string instead of char array and use std::vector<>/std::array to store them. For example:
std::array<std::string,2> calculators =
{
"Pythagorean Theorem",
"Homer's Formula"
};
and for loop can be:
for (int i = 0; i < 2; ++i)
std::cout << "Option " << i+1 << ": " << calculators[i] << std::endl;

C++ long integer oddities?

I'm brushing up on some beginner's algorithms as I familiarize myself with C++. There are already some bugs I have no idea where to start fixing.
1) The following gives me seg faults around the time when the fib() function returns its results. EDIT: with inputs >= 9
#include <iostream>
using namespace std;
int fib(int n) {
int fibs[] = {1, 1}; // dynamic array
int i = 2; // start at 3rd element
while(i < n) {
fibs[i] = fibs[i-2] + fibs[i-1];
cout << "DEBUG: fibs[" << i << "] = " << fibs[i] << endl;
i = i+1;
}
cout << "about to return to main()" << endl;
return fibs[n-1];
}
int main() {
cout << "\n================= Hello cruel world =================" << endl;
cout << "Enter a number: ";
int x;
cin >> x;
cout << "fib(" << x << ") = " << fib(x) << endl;
cout << "================ Goodbye cruel world ================\n" << endl;
return 0;
}
Otherwise, the code works just fine, the numbers are found correctly. But 2) when I change the function to support long integers, it starts acting weird:
#include <iostream>
using namespace std;
long fib(int n) {
long fibs[] = {1L, 1L}; // dynamic array
int i = 2; // start at 3rd element
while(i < n) {
fibs[i] = fibs[i-2] + fibs[i-1];
cout << "DEBUG: fibs[" << i << "] = " << fibs[i] << endl;
i = i+1;
}
cout << "about to return to main()" << endl;
return fibs[n-1];
}
int main() {
cout << "\n================= Hello cruel world =================" << endl;
cout << "Enter a number: ";
int x;
cin >> x;
cout << "fib(" << x << ") = " << fib(x) << endl;
cout << "================ Goodbye cruel world ================\n" << endl;
return 0;
}
Output:
================= Hello cruel world =================
Enter a number: 7
DEBUG: fibs[2] = 2
DEBUG: fibs[0] = 1
DEBUG: fibs[1] = 30071067265
DEBUG: fibs[2] = 30071067266
DEBUG: fibs[14] = 0
about to return to main()
fib(7) = 140733637791872
================ Goodbye cruel world ================
It doesn't make any sense to me. Thanks for any help.
int fibs[] = {1, 1};
is equivalent to:
int fibs[2] = {1, 1};
In another word, the array fibs(in both programs) has only two elements, it's illegal to access fibs[n] if n is bigger than 1.
In
long fibs[] = {1L, 1L};
the [] does not mean "as big as it needs to be", it means "count the number of initializers". It's the same as
long fibs[2] = { 1L, 1L };
And the comment
// dynamic array
is just plain WRONG.
The other answers explain what's wrong. To fix it, you can declare fibs as a vector instead:
vector<int> fibs(n, 1);
which will construct a vector of n integers and initialize them all to 1. Replacing that single line of code should be all you need!
Yes it will fail because fibs[2] doesn't exist!
This is not dynamic array by any mean. Its array of 2 long
long fibs[] = {1L, 1L}; // dynamic array

C++ Recursive functions

I'm learning C++ and I have trouble with getting recursion working when a function is called by itself.
#include <iostream>
using namespace std;
int countdown(int y) {
if (y==1) {
return 1 && cout << y << endl;
}
else {
return countdown(y-1);
}
}
int main () {
cout << "Countdown from ten: " << endl;
cout << countdown(10) << endl;
}
Of course there are other ways to achieve this, but really I created this example to verify my own understanding of how functions are called recursively.
In the example I added && cout << y to verify if y is being passed to the function as 1, which always appears to be the case irrespective that I call the function as countdown(10).
Could someone tell me if I'm missing something obvious here please?
Your ' cout << y ' only executes if y has been tested to be one.
This version does what I think you want:
#include <iostream>
using namespace std;
int countdown(int y)
{
cout << y << endl;
if (y==1)
{
return 1;
}
else
{
return countdown(y-1);
}
}
int main()
{
cout << "Countdown from ten: " << endl;
cout << countdown(10) << endl;
}
Your call stack looks like this:
main
countdown(10)
countdown(9)
countdown(8)
countdown(7)
countdown(6)
countdown(5)
countdown(4)
countdown(3)
countdown(2)
countdown(1)
std::cout << 1 << std::endl;
If you want to see the whole countdown, move the output command in front of the if condition.
Also, your style of writing the output is very unidiomatic. Note that it only works because 1 %&& cout converts the cout to bool and bool can be converted to int. Please don't write code like that.