Ok...so I'm having some trouble with this one. I think I have the right idea, I'm just not sure exactly how to do it.
The program needs to accept 2 numbers of up to 512 characters and add them together, then subtract the second number from the first.
Basically I feel like the best way to tackle this program is to cin the numbers as strings, then read each character into each part of the array...but in order to properly add and subtract the numbers they need to properly line up. I feel like I need to reverse the arrays so that I can put the last number in the last position of the array (i.e. firstNmbr[512] should be the last digit). I've been searching the internet for answers and can't find one, so I've turned to the forum for help. I'll put in what code I have so far, I know some of it is wrong but I just need help with this first problem...then we can move on from there.
forgot to mention that I can only use the iostream and string headers, no cmath or fstream :( that's what makes it much more difficult
// Nate Hoover
// Repetition Advanced
// Sources:
// This program will create and manipulate repetition control structures
#include<iostream>
#include<string>
usingnamespace std;
// init arrays
int firstNmbr[512];
int secondNmbr[512];
// function prototypes
int firstNumber(int n);
int secondNumber(int n);
void stringToArray(string n);
int main(){
// init variables
string firstNmbrString, secondNmbrString;
// Print program and author to screen
cout << "Program: Advanced Repetition" << endl << endl
<< "Author: Nate Hoover" << endl << endl;
// Prompt the user and accept a positive whole number of up to 512 digits
cout << "Enter a whole number: ";
// this was an attempt to put the digits directly into the array
for (int i = 0; i < 512; i++){
getchar(firstNmbr[i]);
}
// function to put string into array
stringToArray(firstNmbrString);
// Prompt the user and accept a second positive whole number of up to 512 digits whose value is less than the first number
cout << "Enter a whole number: ";
cin >> secondNmbrString;
// function to put string into array
stringToArray(secondNmbrString);
// Display the sum of the two numbers
// display difference of the second number subtracted from the first number
// pause and exit
getchar();
getchar();
return 0;
}
// string to array function
void stringToArray(string n){
while (!n.end){
for (int i = 0; i < 512; i++){
getchar();
}
}
}
// function to put first number into firstNmbr array
int firstNumber(int n){
}
ok...still haven't gotten any help, but this is what I have so far. Got about 50 minutes before it's due (I know I'm a procrastinator, but whatever...I get partial credit so I'm going to turn it in regardless, but would still like some insight). Thanks for any help
// Nate Hoover
// Repetition Advanced
// Sources:
// This program will create and manipulate repetition control structures
#include<iostream>
#include<string>
usingnamespace std;
// init arrays
int firstNmbr[512] = { 0 };
int secondNmbr[512] = { 0 };
int dummyArray[512] = { 0 };
int revDummyArray[512] = { 0 };
int sum[512] = { 0 };
int difference[512] = { 0 };
// function prototypes
int firstNumber(int n);
int secondNumber(int n);
void stringToArray(string n);
int add(int n);
int subtract(int n);
int main(){
// init variables
string firstNmbrString, secondNmbrString;
// Print program and author to screen
cout << "Program: Advanced Repetition" << endl << endl
<< "Author: Nate Hoover" << endl << endl;
// Prompt the user and accept a positive whole number of up to 512 digits
cout << "Enter a whole number: ";
cin >> firstNmbrString;
// function to put string into array
stringToArray(firstNmbrString);
// Prompt the user and accept a second positive whole number of up to 512 digits whose value is less than the first number
cout << "Enter a whole number: ";
cin >> secondNmbrString;
// function to put string into array
stringToArray(secondNmbrString);
// Display the sum of the two numbers
add();
// display difference of the second number subtracted from the first number
subtract();
// pause and exit
getchar();
getchar();
return 0;
}
// string to array function
void stringToArray(string n){
while (!n.end){
// this will put the chars into the dummy array
for (int i = 0; i < 512; i++){
int d = 511;
dummyArray[i] = getchar();
revDummyArray[i] = dummyArray[d];
d--;
}
}
}
// function to put first number into firstNmbr array
int firstNumber(int n){
for (int i = 0; i < 512; i++){
int d = 511;
firstNmbr[i] = revDummyArray[d];
d--;
}
}
// function to put second number into secondNmbr array
int secondNumber(int n){
for (int i = 0; i < 512; i++){
int d = 511;
secondNmbr[i] = revDummyArray[d];
d--;
}
}
int add(int n){
for (int i = 511; i >= 0; i--) {
sum[i] = firstNmbr[i] + secondNmbr[i];
cout << sum[i];
}
}
int subtraction(int n){
for (int i = 511; i >= 0; i--) {
difference[i] = firstNmbr[i] - secondNmbr[i];
cout << difference[i];
}
}
stringToArray(string n) is wrong. n can be used as an array.
This is how it could be done:
1 2 3 4 5 6 7 8
void stringToArray(int *array_to_store_values, string n){
for(size_t i = 0; i < n.size(); ++i){
array_to_store_values[i] = n[n.size() - i - 1] - '0';
// Note that "- '0'" converts the character '0' into int 0 (and '1' -> 1 etc.)
// n[n.size() - i - 1] stores the least significant number first
}
}
}
It is use like so:
1 2 3 4 5 6
// Prompt the user and accept a positive whole number of up to 512 digits
cout << "Enter a whole number: ";
cin >> firstNmbrString;
// function to put string into array
stringToArray(firstNmbr, firstNmbrString);
Note that you cannot use tha 'magic' number like 511 or 512. You need to use the string size instead.