-5

I will have to take the input as a string.Then turn it to an integer.Then use it in calculations.

    #include <iostream>
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        string word;
        cin>>word;
        word=stol(word); 
        cout<<word;
        return 0;
    }
  • What is your question? Do you want to read an entire line? Use `std::getline`, if that is the case. – Algirdas Preidžius Mar 14 '18 at 00:06
  • Why do you want spaces for your use case? To get them, use unformatted input operations like `std::getline()`. Also, **always** check if input was successful after the read operation, e.g., `if (std::cin >> word) { /* use the read data */ }` – Dietmar Kühl Mar 14 '18 at 00:06
  • 2
    Unrelated, but you should avoid using `include ` because it just includes way more than you need, and you won't properly learn what to include and _why_ – Tas Mar 14 '18 at 00:09
  • 4
    Possible duplicate of [How to parse a string to an int in C++?](https://stackoverflow.com/questions/194465/how-to-parse-a-string-to-an-int-in-c) – Tas Mar 14 '18 at 00:10
  • Did you bother to make some search before asking your question? There are many questions on SO that asked exactly the same thing that you're asking. – Xam Mar 14 '18 at 05:22

1 Answers1

0

do not store it in word again because you declared word at first as string so instead create another variable of type( int/float/double) and do the "stoi(word)" (add this to your code and remove the word=stoi(word)" )

here is full code :

#include <iostream>
#include <string>
using namespace std;
int main()
{
  int x;
  string word;
  cin>>word;
  x=stoi(word);
  //now you can use x in any mathematical operation you want
  //try couting the x or (add, multiply,...etc to it)
}