string problem

hi!
I don't know what's the problem with this code????
I can't see the result of my third test cases about cancatenation strings with + on screen
the output is just the first string I initialized
please helping me
I'm using online compiler

#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
int main()
{
int i = 4;
double d = 4.0;
string s = "i'm a";
int I;
double D;
string s1;
cin>>I;
cin>>D;
getline(cin,s1);
cout<<I+i<<endl;
D=D+d;
printf("%.1lf\n",D);
s+=s1;
cout<<s;
return 0;
}
Last edited on
Hi there,
Firstly, why are you using an online compiler?
Do you have a real "offline" compiler in your pc?
First of all, use code tags please :P

try including <string> before iostream.
also, you getline takes what you've entered on D i guess?
i modified it a bit and on Visual Studio 2013 it works like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <string>    //i already include that on habit, altough im also fairly new
                                //to C++
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
int main()
{
	int i = 4;
	double d = 4.0;
	string s = "i'm a";
	int I;
	double D;
	string s1;

	cout << "I" << endl;
	cin >> I;
	
        cout << "D" << endl;
	cin >> D;
	
        cout << "s1" << endl;
	cin.ignore();  //seems to fix the issue ;)
	getline(cin, s1);
	
        cout << I + i << endl;
	D = D + d;
	printf("%.1lf\n", D);
	s += s1;
	cout << s;
	return 0;
}

There may not be any problem in your code. What matters is that some online compilers may not produce an executable file upon success for download so that you can test and see your program results in a more informative and interactive way. Please correct me if I am wrong.
Last edited on
on C++ Shell it is running the same way as it is on Visual Studio, just tried that.

also with the same corrections as i did, its showing the same results as on Visual Studio.

Try the Code i posted on your online Compiler if you don't use cpp.sh and see if that works. If it doesn't, it's the compiler.
If you type the input all on a single line, for example,
2 3 fred
then the original code works - almost. (it picks up a leading space at the start of the string).

However, if the input is entered and a newline is entered after each value, then the getline doesn't work as required. That's because after entering the value for D, there is a trailing newline still in the input buffer. (there may be some other characters such as spaces if that's what the user typed). To remove those unwanted characters, after cin>>D; add the extra line,
 
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n');
this will remove everything up to and including the newline character. Then the getline will work.
Another C style way is to declare a c-string for input but may not be very recommended...

1
2
3
4
char stringInput[250];

cin >> stringInput;
s1 = stringInput;


But it will always work, for sure.

Edit : Knowing that means you remove this getline command. Use one of these, don't use both!
Last edited on
cout >> stringInput;
o_O
Removing the getline() will alter the functionality. Only do that if this meets the program requirements. But don't use c-strings for this purpose, you can use a std::string with cin >> and is safer than using a c-string.

VRGaming2 wrote:
But it will always work, for sure.
It has risks, it may not work, for example if the user holds down a key and enters a very long string, it will fail, or cause unpredictable behaviour due to buffer overflow.
programmer007 wrote:
cout >> stringInput;


LMFAO how stupid of me XD
thank you all
1
2
3
 
     std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n');
  

I just include that after cin>>D
and it works

:)))))))))))))))))
Topic archived. No new replies allowed.