Question

I have written this function in C++. A string of integers is passed in as dString and an integer is passed in as count. each dString integer is then indexed and passed into strTolnt integer array. each strTolnt integer is then passed through an equation and stored in temp array. I have printed each index in the array and the correct values are displayed, but when I print the entire array the value is displayed as hex. temp array is then passed back into dString, then dString is returned via stoi), when I print dString boxes are displayed. I want the result, in this case 5555 to be returned via dString as an integer 6 ainclude <string» 10 /Function that iapl ements modulo equation encryption 11 dstring passed in fron deteString and 13 int equatien encrypt istring dString, iet count) / /Declere temp erray of length count int tesp[count J: int str int bi cout << Fron equation-dstrirg: くく dstring 《< endl; //For Debugging for. (irt í @g 1 < count; İ++) /1 Convert each indeed string into a. integer strToInt[1)- stoiCa); for loop cycling froe count to a and1 from count to 1,1 represents posit1on of digit read from right to lef for (int í =e, j = count; ǐくcourt, j 1;i++, j-) cou << aqisation dStrlng[ <c ΐ << ) << dString[1] ead1; //For Debuggl ng /Result of equation for each indexed dstring stored in temo array tmp,[1]-(MrTutnt[3].J) % 10İ //et.uetlun rur mtrypt lur cout << equation i- i endl; //For Debugging e4 endl .temp 11 くく end1; //Far Debugging cout << ation teap array: << tespくく endL; //For Debugg for (int i-e;i < count; i++) eString is ass1gned the encrspted value stored In tenp array and converted to string/ b - temp[i dstringri1 b t e equation encrypted string: < dString < endl;yFor Debuggin return stol(dstring 11) Enter data for encryptaf Decrypt stored data Please mter data Cother than 0): 1234 Fron nain datastri equation dstring]i equation dstringti] equationi equation dStrinac temp [2] 5 equationj-1 quation temp array: ox61fedc erminete called after throwing an instance ef stdiaimalid ument what O: stoi 0 128

0 0
Add a comment Improve this question Transcribed image text
Answer #1

You cannot store directly int number in string[0]. In dString when you are assign to b value that is not storing.So in dString string is empty when you are converting stoi(dSting) thats why that is showing that error. now i changed that.can you check modify your code in 3rd for loop.

code:

#include<iostream>

#include<string>

using namespace std;

int equation_encrypt(string dString,int count)

{

int temp[count];

int strToInt[count];

string a;

int b;

cout <<"from equation - dString" << dString <<endl;

for(int i=0;i<count;i++)

{

a=dString[i];

strToInt[i]=stoi(a);

}

for(int i=0,j=count;i<count,j>=1;i++,j--)

{

cout << "equation dString [" << i << "]" << dString[i] << endl;

temp[i] = (strToInt[i]+j)%10;

cout << "temp[" << i <<"]"<<temp[i] <<endl;

}

cout << "equation temp array " << temp << endl;

for(int i=0;i<count;i++)

{

string c = to_string(temp[i]);

cout << c<<endl;

dString[i] =c[0];

cout << "equation encrypted char :" <<dString[i] << endl;

}

cout << "equation encrypted string :" << dString << endl;

return stoi(dString);

}

int main(void)

{

cout <<"after conversion string is = " <<equation_encrypt("1234",4) <<endl;//testing purpose

}

output:

from equation - dString1234 equation dString [0]1 temp[0]5 equation dString [112 temp[1]5 equation dString [2]3 temp[2]5 equation dstring [314 temp[3]5 equation temp array ox7ffe5fe78830 equation encrypted char :5 equation encrypted char :5 equation encrypted char :5 equation encrypted char :5 equation encrypted string :5555 after conversion string is -5555

Add a comment
Know the answer?
Add Answer to:
I have written this function in C++. A string of integers is passed in as dString...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • My following program has an array which holds 1000 random integers between 1-1000. Now I need...

    My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...

  • Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters...

    Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters student first name, 10 characters middle name, 20 characters last name, 9 characters student ID, 3 characters age, in years (3 digits) 2- Open the input file. Check for successful open. If the open failed, display an error message and return with value 1. 3- Use a pointer array to manage all the created student variables.Assume that there will not be more than 99...

  • can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define...

    can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define SIZE 100 using namespace std; //declare struct struct word_block {    std::string word;    int count; }; int getIndex(word_block arr[], int n, string s); int main(int argc, char **argv) {    string filename="input.txt";    //declare array of struct word_block    word_block arr[SIZE];    int count = 0;    if (argc < 2)    {        cout << "Usage: " << argv[0] << "...

  • How can I make this compatible with older C++ compilers that DO NOT make use of...

    How can I make this compatible with older C++ compilers that DO NOT make use of stoi and to_string? //Booking system #include <iostream> #include <iomanip> #include <string> using namespace std; string welcome(); void print_seats(string flight[]); void populate_seats(); bool validate_flight(string a); bool validate_seat(string a, int b); bool validate_book(string a); void print_ticket(string passenger[], int i); string flights [5][52]; string passengers [4][250]; int main(){     string seat, flight, book = "y";     int int_flight, p = 0, j = 0;     int seat_number,...

  • For each C++ function below, give the tightest can asymptotic upper bound that you can determine....

    For each C++ function below, give the tightest can asymptotic upper bound that you can determine. (a) void mochalatte(int n) { for (int i = 0: i < n: i++) { count < < "iteration;" < < i < < end1: } } (b) void nanaimobar (int n) { for (int i = 1: i < 2*n: i = 2*i) { count < < "iteration;" < < i < < end1: } } void appletart (int n) { for (int...

  • I want to compare the runtimes and swap operations times among quick Sort, selection Sort and...

    I want to compare the runtimes and swap operations times among quick Sort, selection Sort and shell Sort here is my code: But when I create a 1000,000 size array, I can't get the result of the operations times and runtime. what's wrong with my code? and I also want to copy the array. Because I want to use same array for three sort. And for the shell Sort, I haven't learn it in my class. Can anyone help me...

  • Need help with a C++ program. I have been getting the error "this function or variable...

    Need help with a C++ program. I have been getting the error "this function or variable may be unsafe" as well as one that says I must "return a value" any help we be greatly appreciated. I have been working on this project for about 2 hours. #include <iostream> #include <string> using namespace std; int average(int a[]) {    // average function , declaring variable    int i;    char str[40];    float avg = 0;    // iterating in...

  • I need help fixing my code: In C++ *************** 1) I want to sum the digits...

    I need help fixing my code: In C++ *************** 1) I want to sum the digits of an n*n matrix 2) find the average I have completed the rest ****Do not use C++ standard library. You must use pointers and pointer arithmetic to represent the matrix and to navigate through it. MY CODE: (I have indicated at which point I need help) #include <iostream> using namespace std; void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp;...

  • Problem with C++ program. Visual Studio say when I try to debug "Run-Time Check Failure #2...

    Problem with C++ program. Visual Studio say when I try to debug "Run-Time Check Failure #2 - Stack around the variable 'string4b' was corrupted. I need some help because if the arrays for string4a and string4b have different sizes. Also if I put different sizes in string3a and string4b it will say that those arrays for 3a and 3b are corrupted. But for the assigment I need to put arrays of different sizes so that they can do their work...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT