How to convert an char array to double value in c++? - c++

importing library
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <cmath>
#include <iostream>
#include <stdlib.h>
#include <cstring>
#include <math.h>
#include <stdlib.h>
#include <typeinfo>
using namespace std;
const int size = 100;
getting the user input for dollar amount
int main()
{
void mstold(char[]);
char array[size];
cout << "enter the amount" << endl;
cin >> array;
// cout<<strlen(array)<<endl;
mstold(array);
}
mstold function to extract only the digits and decimal from the user given dollar amount
void mstold(char array[])
{
int count = 0;
// char pure[strlen(array)];
int index_pure = 0;
// counting the number of significant values which are to be extracted
for (int i = 0; i < strlen(array); i++)
// if ((int(array[i]) < 58 and int(array[i]) > 48) or (array[i] == ','))
if ((int(array[i]) < 58 and int(array[i]) > 48) or (array[i] == '.'))
{
count++;
}
cout << "count" << endl;
cout << count << endl;
char pure[count + 1];
for (int i = 0; i < strlen(array); i++)
// if ((int(array[i]) < 58 and int(array[i]) > 48) or (array[i] == ','))
if ((int(array[i]) < 58 and int(array[i]) > 48) or (array[i] == '.'))
{
pure[index_pure] = array[i];
cout << int(array[i]) << endl;
++index_pure;
}
pure[index_pure] = '\0';
cout << "pure" << endl;
// created a array name pure which contains only digit and decimal.
// however this is an array.
// i need to convert this array of char to a double type variable.
cout << pure << endl;
// this line causing error
long double money = _atof_l(pure);
cout << money << endl;
// atoi is working and converting the string array to int
// however the array to double is not working
}

Related

Code won't run, getting stuck at while loop

When I try to run the program visual studio code gets stuck at the while look and won't run the code. When copy and pasting the code to an online compiler the code runs. Also I can't use vectors or reverse from the library.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <fstream>
#include <ctime>
using namespace std;
void reverse(double* a, int size)
{
double* left = a;
double* right = a + size - 1;
while (left < right)
{
double temp = *left;
*left++ = *right;
*right-- = temp;
}
}
int main()
{
int size = 0;
float input;
double* a = new double[size];
int capacity = 10;
cout << "Please enter the values: ";
while (cin >> input)
{
if (cin.fail())
{
cin.clear();
}
if (size == capacity) {
capacity *= 2;
}
a[size++] = input;
if (size == capacity)
{
capacity *= 2;
double* b = new double[capacity];
for (int i = 0; i < size; i++)
{
b[i] = a[i];
}
delete[] a;
a = b;
}
}
cout << "The input values are: ";
for (int i = 0; i < size; i++)
{
cout << a[i];
if (i < size - 1)
cout << ", ";
}
cout << endl;
reverse(a, size);
cout << "The reversed values are: ";
for (int i = 0; i < size; i++)
{
cout << a[i] << " ";
if (i < size - 1)
cout << ", ";
}
cout << endl;
return 0;
}
I tried using a break command it didn't work. I'm expecting the code to run the function and take in all the inputs and print the array and its reverse
can you check if this is the answer you wanted, I made the input variable integer type where you were using float/double type, I thought it is the problem, also you did not update the input in the while loop so I used while(input--) . These are the changes I have made I do not know if this is the answer you wanted. I added the comment where I made changes to the code. Hope it is helpful.
#include <iomanip>
#include <cmath>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <fstream>
#include <ctime>
using namespace std;
void reverse(double* a, int size)
{
double* left = a;
double* right = a + size - 1;
while (left < right)
{
double temp = *left;
*left++ = *right;
*right-- = temp;
}
}
int main()
{
int size = 0;
int input; // I made input variable integer type you were using float type
double* a = new double[size];
int capacity = 10;
cout << "Please enter the values: ";
cin >> input ; // took the value of the input variable here
while (input--) // you did not decrement the input variable
{
if (cin.fail())
{
cin.clear();
}
if (size == capacity) {
capacity *= 2;
}
a[size++] = input;
if (size == capacity)
{
capacity *= 2;
double* b = new double[capacity];
for (int i = 0; i < size; i++)
{
b[i] = a[i];
}
delete[] a;
a = b;
}
}
cout << "The input values are: ";
for (int i = 0; i < size; i++)
{
cout << a[i];
if (i < size - 1)
cout << ", ";
}
cout << endl;
reverse(a, size);
cout << "The reversed values are: ";
for (int i = 0; i < size; i++)
{
cout << a[i] << " ";
if (i < size - 1)
cout << ", ";
}
cout << endl;
return 0;
}```

Converting an Ascii Character to an Integer

Hey guys I have a program here that basically needs a key to open encrypted data but for some reason every time I run the program when converting the Ascii numbers to characters I get characters with the wrong Ascii value. For example in the code below if it tried converting the Ascii Value '50' to a char I would get 'e' instead of '2'. Any help would be much appreciated.
#include "stdafx.h"
#include "iostream"
#include "string"
#include <cstdlib>
#include <ctime>
#include <algorithm>
using namespace std;
string key = "5053525055";
int asciiValues;
char asciiChars;
for (int i = 0; i < key.length(); i += 2)
{
asciiValues = (int)(key[i] + key[i + 1]);
asciiChars = (int)(key[i] + key[i + 1]);
}
Here's the complete code for those interested.
#include "stdafx.h"
#include "iostream"
#include "string"
#include <cstdlib>
#include <ctime>
#include <algorithm>
using namespace std;
void encryption(string text)
{
char asciiChar;
int asciiValue = 0;
string key;
string encoded;
srand((unsigned)time(0));
int random_integer = rand();
cout << random_integer << endl;
//Creates the key for the string.
for (int i = 0; i < to_string(random_integer).length(); i++)
{
int asciiValue = (char)(to_string(random_integer)[i]);
key = key + to_string(asciiValue);
cout << asciiValue << endl;
}
int help = to_string(asciiValue).length();
/*Function that converts the individual characters in the input
string to AsciiValues and then puts them into the encoded data.*/
for (int i = 0; i < text.length(); i++)
{
int asciiValue = char(text[i]) + random_integer;
encoded = encoded + to_string(asciiValue) + ".";
}
cout << "Encrypted data: " << encoded << endl;
cout << "Your key for this encoded data is " << key << endl;
}
void decryption(string text, string key)
{
char asciiChars;
int asciiValues;
int number;
string qkey;
string decoded;
/*for (int i = 0; i < to_string(random_integer).length(); i++)
{
int asciiValue = (char)(to_string(random_integer)[i]);
key = key + to_string(asciiValue);
cout << asciiValue << endl;
}*/
for (int i = 0; i < key.length(); i += 2)
{
asciiValues = (int)(key[i] + key[i + 1]);
asciiChars = (int)(key[i] + key[i + 1]);
number = asciiChars - '0';
cout << number << endl;
}
cin >> qkey;
}
int main()
{
string answer;
int question = 0;
string vkey;
ask:
cout << "Would you like to:\nEncrypt Data[1]\nDecrypt Data[2]\nExit[3]" <<
endl;
cin >> question;
if (to_string(question) != "1"&&to_string(question) !=
"2"&&to_string(question) != "3")
{
goto ask;
}
else if (to_string(question) == "1")
{
while (answer.length() > 1000 || answer.length() < 1)
{
cout << "Please enter a string that has a length of 1 to 1000
characters. ";
cin >> answer;
cout << endl;
}
encryption(answer);
cin >> answer;
goto ask;
}
else if (to_string(question) == "2")
{
cout << "Please enter the string you would like decrypted. ";
cin >> answer;
cout << endl;
cout << "Now please enter the key for the string. ";
cin >> vkey;
cout << endl;
decryption(answer, vkey);
}
return 0;
}
ASCII number characters begin at 30hex (or 48dec). Therefore '0' = 30hex. So, to get an ASCII character, you must add '0' (30h).
'cout << 5 + '0'; // 53dec (35hex)

I'm getting an error that the debug assertion failed?

I am trying to read in a string from the user, change it to an int array and then display it int by int. This is so I can get very large int numbers, but when I run it, it lets me enter a number, but then it gives me an error that the debug assertion failed and that I should abort, retry, or ignore. There is an invalid null pointer. I don't know what means. Here is what I have:
//LargeInt.h File
#pragma once
#include <iostream>
#include <string>
using namespace std;
class LargeInt
{
private:
string number1;
string number2;
string sum;
public:
LargeInt();
~LargeInt();
//This function will take the
//the number entered by user
//as int digits and print out
//each digit.
void ReadNumber(int[]);
};
//LargeInt.cpp file
#include "stdafx.h"
#include "LargeInt.h"
#include <iostream>
LargeInt::LargeInt() {
number1 = " ";
}
LargeInt::~LargeInt() {
}
//This function will take the
//the number entered by user
//as int digits and print out
//each digit.
void LargeInt::ReadNumber(int
number[]) {
for (int i = 0; i < 75; i++) {
cout << number[i]; }
}
//Main File
#include "stdafx.h"
#include <string>
#include "LargeInt.h"
#include <iostream>
using namespace std;
int main()
{
string number1 = " ";
string number2 = " ";
string sum = " ";
int summ[75];
//Get number 1 from user and output it
int numberOne[76] = {};
cout << "Enter first number: ";
getline(cin, number1);
cout << endl;
//Check to see if it is more than 75 digits
if (number1.length() > 75) {
cout << "Invalid number!" << endl;
}
else {
int length1 = number1.length();
cout << "First Number: " << endl;
int k = 75;
for (int i = length1; i >= 0; i--) {
numberOne[k] = number1[i] - 48;
k--;
}
}
LargeInt num1;
num1.ReadNumber(numberOne);
cout << endl;
system("pause");
return 0;
}
int length1 = number1.length();
Is going to set length1 to the size of the string. Then in the first iteration of
for (int i = length1; i >= 0; i--) {
numberOne[k] = number1[i] - 48;
k--;
}
You are accessing number1[i] with i = length1 and since string positions are 0 based you are accessing on past the end of the string. This is undefined behavior and in this case an assertion is being thrown. To fix this you need to set i to length1 - 1.

Program prints all digits from array, prints backwards

I'm making a program that prints all digits from an array (entered as an integer) and it works, but the digits are printed backwards and I don't know how to reverse them. Can someone help?
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
void explode(int number,int array[])
{
while (number > 0) {
int digit = number % 10;
cout << digit << '\n';
number /= 10;
}
}
int main()
{
int digits[100];
int numdigits;
int n;
cout << "Enter number: ";
cin >> n;
// numdigits = explode(n,digits);
cout << "[";
while (n > 0) {
int digit = n % 10;
n /= 10;
digits[digit] = digit;
cout << digits[digit];
}
cout << "]" << endl;
}
You just have to reverse the array using reverse() from <algorithm>.
Code:
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
using namespace std;
int array_c = 0;
void explode(int number,int array[])
{
while (number > 0) {
int digit = number % 10;
number /= 10;
array[array_c++] = digit;
}
}
int main()
{
int digits[100];
int numdigits;
int n;
cout << "Enter number: ";
cin >> n;
explode(n,digits);
reverse(digits,digits+array_c);
cout << "[";
for(int i = 0; i < array_c; ++i)
cout<<digits[i];
cout << "]" << endl;
}
Your use of
digits[digit] = digit;
is not right. You probably meant to use
digits[numdigits] = digit;
You can fix your problem by dividing the work into two steps. In the first step, you store the digits. In the second step, you print the digits.
int numdigits = 0;
while (n > 0) {
cout << "n: " << n << endl;
int digit = n % 10;
n /= 10;
digits[numdigits++] = digit;
}
// Make sure to print them in reverse order.
cout << "[";
for ( ; numdigits > 0; )
{
cout << digits[--numdigits];
}
cout << "]" << endl;

Count Occurrences within a for loop c++

I'm new to c++, what i'm trying to do is count the amount of times a letter occurs with a piece of text or paragraph and stores this into an array called frequency array.
The code below is working to a degree, what happens is if the user types hello frequencyarray stores 11121, if the user types aaba frequency array stores 1213
I don't want a running total i'm wanting the array to store 1121 and 31. so if the same letter appears it adds 1 to the array.
Thanks David
#include <iostream> //for cout cin
#include <string> //for strings
#include <fstream> //for files
using namespace std;
int main()
{
string text;
int frequencyarray [26]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
cout << "Enter Word: ";
cin >> text;
//***************************COUNT OCCURANCES************************
for (int i(0); i < text.length(); i++)
{
char c = text[i];
c = toupper(c);
c -= 65;
if (c < 26 || c >=0)
{
frequencyarray[c]++;
cout << frequencyarray[c];
}
}
system ("pause");
return(0);
}`
If you don't want the running total, don't have the cout << freqencyarray[c]; inside the cycle that is counting the occurrences.
Try this
#include <iostream> //for cout cin
#include <string> //for strings
#include <fstream> //for files
#include <algorithm>
using namespace std;
int main()
{
string text;
int frequencyarray [26]={0};
cout << "Enter Word: ";
cin >> text;
//***************************COUNT OCCURANCES************************
for (int i(0); i < text.length(); i++)
{
char c = text[i];
c = toupper(c);
c -= 65;
if (c < 26 || c >=0)
{
frequencyarray[c]++;
}
}
std::for_each(std::begin(frequencyarray), std::end(frequencyarray), [](int i)
{
std::cout << i << ",";
});
std::cout << "\n";
system ("pause");
return(0);
}