I'm pretty knew to C++. I'm writing a calculator primarily based upon vectors. I want to have that there is a vector for each operation, and if one of the following operations is called, the value will manipulated by the associated vector. Questions 1. Is my process correct? Am I on the right track? 2. Do I need an alternative method for inputting and checking for a char and an int?
#include <stdio.h>
#include <iostream>
#include <vector>
#include <numeric>
#include <string>
#include <functional>
using namespace std;
vector <int> addition;
vector <int> subtraction;
vector <int> division;
vector <int> multiplication;
char operation;
int input;
int main() {
cout << "Welcome to my Calculator Program" << endl;
cout << "Enter your calculation: ";
void operation();
return 0;
}
void operation() {
cin >> operation;
switch (operation) {
case '+':
addition.push_back(input);
break;
case '-':
subtraction.push_back(input);
break;
case '/':
division.push_back(input);
break;
case '*':
multiplication.push_back(input);
break;
default:
cout << "You have entered an invalid operation" << endl;
}
}
int add(vector <int> addition) {
for (int i; i > addition.size(); i++) {
int sum = std::accumulate(addition.begin(), addition.end(), 0);
}
return sum;
}
int sub(vector <int> subtraction) {
for (int i; i > subtraction.size(); i++) {
}
}
It looks like you're on the right track to me. To answer your second question, no, cin>>variable;
automatically attempts to convert user input to the type of variable
.
The main problem with this program is that it will not follow order of operations, which makes it pretty useless as a calculator.
You aren't calling operation()
when you do void operation();
; you're declaring it. Nothing will happen except a compiler warning.
The whole idea of std::accumulate() is to avoid looping through a vector or array! Reevaluate if you really need those for loops.
Fix your indentation. The way it is now makes it hard to understand the program's flow. Check out: https://www.cs.northwestern.edu/academics/courses/211/html/c++-style.html#indent
Don't use global variables when you can avoid it, which you definitely can in this case. In this program, it doesn't matter, but it's a really bad habit.
You are using namespace std
but still referring to things as std::whatever
. That's redundant.
I think you should go back to the textbook and work through some simpler examples. Your logic is (mostly) sound, but you lack a grasp of the syntax. That's nothing to be ashamed of -- C++ is hard and, as you say, you're new -- but you really need to understand that before working on anything more complex than basic examples.
Good luck!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.