// infinitesumtrial1.cpp: Konsol uygulamasının giriş noktasını tanımlar.
//
#include "stdafx.h"
#include <iostream>
double absolute(double x)
{
if (x < 0)
return -x;
return x;
}
double infinitesum(double a, double r)
{
longdouble epsilon = 1e-10;
longdouble sum = 0;
double x = r;
/*for (a, r; r < epsilon; r *= r) //not working edit: lol becasuse r<epsilon
sum += a;
*/
//double y= abs(r);
while (absolute(r)> epsilon)
{
sum += a * r;
r *= x;
}
return sum;
}
int main()
{
std::cout << "INFINITE SUM TRIAL 1 \n";
std::cout << "Geometric Sum \n";
std::cout << "Define a value for a: ";
double a;
char q;
std::cin >> a;
std::cin >> q;
if (q == '/')
{
double w;
std::cin >> w;
a /= w;
}
std::cout << "Define a value for r: ";
double r;
char t;
std::cin >> r;
std::cin >> t;
if (t == '/')
{
double y;
std::cin >> y;
r /= y;
}
if (a == 0)
std::cout << "The result is 0";
if ((r >= 1 || r<= -1) && a != 0) {
if (a > 0)
std::cout << "The result goes towards positive infinity.\n";
if (a < 0)
std::cout << "The result goes towards negative infinity. \n";
}
if ((r<1 && r>0 ||r>-1 && r<0) && a!=0 )
std::cout << "The result is: " << infinitesum(a, r)<<"\n";
return 0;
}
The problem is that when the user just types in 10.12, that number becomes 1 token put into your double a variable. But then it still attempts to get q. The solution has to be a bit more dynamic than this.
Although I don't see your attempt with getline, you can use it to grab the whole expression (either "10.2" or "2/7") and parse it yourself, appropriately.
I'm not sure if this is the simplest solution, but:
#include <iostream>
#include <string>
#include <stdexcept> // std::invalid_argument
int main()
{
std::string line;
std::getline(std::cin, line);
// if line contains a '/', split the two numbers between it.
// otherwise, assume a decimal number
size_t slash_index = line.find("/");
if (slash_index != std::string::npos)
{
// found a slash, take two substrings
std::string numerator = line.substr(0, slash_index);
std::string denominator = line.substr(slash_index+1);
// convert string to int (then store it in a double)
try
{
double a = std::stoi(numerator);
double b = std::stoi(denominator);
double value = a / b;
std::cout << "Parsed " << a << "/" << b << " = " << value << "\n";
}
catch (const std::invalid_argument& ia)
{
std::cout << "Invalid argument: " << ia.what() << "\n";
// handle conversion error
}
}
else
{
// No slash found, assume it's a decimal (floating-point) number
try
{
double value = std::stod(line);
std::cout << "Parsed " << value << "\n";
}
catch (const std::invalid_argument& ia)
{
std::cout << "Invalid argument: " << ia.what() << "\n";
// handle conversion error
}
}
}
10.12
Parsed 10.12
2/7
Parsed 2/7 = 0.285714
2/
Invalid argument: stoi
/2
Invalid argument: stoi
PS:
1 2 3 4 5
if ((r >= 1 || r<= -1) && a != 0) {
if (a > 0)
std::cout << "The result goes towards positive infinity.\n";
if (a < 0)
std::cout << "The result goes towards negative infinity. \n";
This indentation is misleading.
It should be formatted as
1 2 3 4 5 6
if ((r >= 1 || r<= -1) && a != 0) {
if (a > 0)
std::cout << "The result goes towards positive infinity.\n";
if (a < 0)
std::cout << "The result goes towards negative infinity. \n";
}
unrelated but there are nonstandard ways to trap the enter key, if you really, really need it. Most of the time, you don't. The issue appears if you try to write your own version of, for example, the simple windows program 'pause' (which many people put in programs to freeze the screen, but it was designed for batch file programming I believe). It is extremely difficult to recreate that program using only official c++ commands, and its a 1 line statement using extensions.