output stream exercise

Write your question here.

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
35
36
37
38
39
40
41
42
43
44
#include "../std_lib_facilities.h"

void outotoputto (double a, double b, double c);
void intotoputto (double a, double b, double c);

int main()
{
	//ofstream ofs("prova.txt", ios_base::out);

	cout<<"Please enter a float number:\n";
	double n = 0;
	cin>>n;
	cout<<"Please another one:\n";
	double m = 0;
	cin>>m;
	cout<<"Yet another one:\n";
	double p = 0;
	cin>>p;
	
	intotoputto (n, m, p);
	outotoputto (n, m, p);

	return 0;
}

void outotoputto (double a, double b, double c)
{
	ofstream ofs("prova.txt", ios_base::out);

	ofs << a << '\t' << oct << a << '\t' << hex << a << '\n';
	ofs << b << '\t' << fixed << b << '\t' << scientific << b << '\n';
	ofs << "Let's try to adjust the structure\n";
	ofs << a << setw(14) << oct << a << setw(14) << hex << a << '\n';
	ofs << c << setw(20) << oct << c << setw(20) << hex << c << '\n';
}

void intotoputto (double a, double b, double c)
{
	cout << a << '\t' << oct << a << '\t' << hex << a << '\n';
	cout << b << '\t' << fixed << b << '\t' << scientific << b << '\n';
	cout << "Let's try to adjust the structure\n";
	cout << a << setw(14) << oct << a << setw(14) << hex << a << '\n';
	cout << c << setw(20) << oct << c << setw(20) << hex << c << '\n';
}


when i run this code it let me enter only the first float (n) then it writes al the sentencences without letting me enter the other numbers (m, p), why?

then it doesn't write in octal and hexadecimal format, why?

then if i enter 'general' in the 4th line of both functions which i should to bring back the format to general it says (in compiling phase) that general is an identifier not defined, why?

thanks as always in advance.
The reason the values are using decimal notation and not hexadecimal or octal is because these manipulators are for integer types only, and you use floating-point values. So the oct and hex manipulators will not have any effect on them at all.

General is not a known stream manipulator. You can save and restore the state of the iostream using the copyfmt method of cout. This method can save and restore the format in which your output is printed. Use it to store the state before using the manipulators and restore it afterwards.

What exactly was your input into the program? Your code to read values from cin looks OK to me.
the input was something like 1234,123456 which should be a double so cant imagine why it jumped the last two cin, how do i solve this?

i toke the manipulator general from page 381 of programming principles and practice using C++ by stroustrup, but probably i was a bit deconcentrated when doing the exercise and forgot to add these lines of code the book teaches to enable 'general':

1
2
3
4
5
in line ios_base& general(ios_base& b)
{
        b.setf(ios_base::fmtflags(0), ios_base::floatfield);
        return b;
}


so this problem is solved (still have to test it though), by the way if i would use your code the synthax would be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void outotoputto (double a, double b, double c)
{
        ofstream ofs("prova.txt", ios_base::out);

        ofs << a << '\t' << oct << a << '\t' << hex << a << '\n';
	ofs.copyfmt << b << '\t' << fixed << b << '\t' << scientific << b << '\n';
	ofs << "Let's try to adjust the structure\n";
	ofs.copyfmt << a << setw(14) << oct << a << setw(14) << hex << a << '\n';
	ofs << c << setw(20) << oct << c << setw(20) << hex << c << '\n';
}

void intotoputto (double a, double b, double c)
{
	cout << a << '\t' << oct << a << '\t' << hex << a << '\n';
	cout.copyfmt << b << '\t' << fixed << b << '\t' << scientific << b << '\n';
	cout << "Let's try to adjust the structure\n";
	cout.copyfmt << a << setw(14) << oct << a << setw(14) << hex << a << '\n';
	cout << c << setw(20) << oct << c << setw(20) << hex << c << '\n';
}



right?
A double in C++ is read with a dot, not a comma. Try entering 1234.123456 instead. The comma is not recognized as a valid character, raising an error in cin and causing all operations on it to fail.

I don't think copyfmt works like you provided the last time though. You can either use your general modifier (given you write it in your code), or use it as a method, like so:

1
2
3
4
5
6
ios state(nullptr);
state.copyfmt(cout); //Save the state

cout << a << oct << a << hex << a; // Or any other combination you like

cout.cpyfmt(state); //Restore the state 
ok i have compiled this code and lots of things has been fixed but still getting strange results:

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "../std_lib_facilities.h"

void outotoputto (double a, double b, int c);
void intotoputto (double a, double b, int c);

int main()
{
	//ofstream ofs("prova.txt", ios_base::out);

	cout<<"Please enter a float number:\n";
	double n = 0;
	cin>>n;
	cout<<"Please another one:\n";
	double m = 0;
	cin>>m;
	cout<<"Now enter an int:\n";
	int p = 0;
	cin>>p;
	
	intotoputto (n, m, p);
	outotoputto (n, m, p);

	return 0;
}

void outotoputto (double a, double b, int c)
{
	ofstream ofs("prova.txt", ios_base::out);

	ofs << int(a) << '\t' << oct << int(a) << '\t' << hex << int(a) << '\n';

	ios state(nullptr);
	state.copyfmt(cout); //Save the state

	ofs << dec << b << '\t' << fixed << b << '\t' << scientific << b << '\n';
	
	cout.copyfmt(state); //Restore the state 
	
	ofs << "Let's try to adjust the structure\n";
	ofs << int(a) << setw(14) << oct << int(a) << setw(14) << hex << int(a) << '\n';
	ofs << dec << c << setw(20) << oct << c << setw(20) << hex << c << '\n';
}

void intotoputto (double a, double b, int c)
{
	cout << int(a) << '\t' << oct << int(a) << '\t' << hex << int(a) << '\n';
	
	ios state(nullptr);
	state.copyfmt(cout); //Save the state
	
	cout << dec << b << '\t' << fixed << b << '\t' << scientific << b << '\n';

	cout.copyfmt(state); //Restore the state
	
	cout << "Let's try to adjust the structure\n";
	
	cout << int(a) << setw(14) << oct << int(a) << setw(14) << hex << int(a) << '\n';
	cout << dec << c << setw(20) << oct << c << setw(20) << hex << c << '\n';
}


these were the input numbers:
23.4356
5678.12345
20

and this is the output:

23 27 17 \\which aren't neither octal nor hexadecimal so what are and why?
5678.12 5678.123450 5.678123e+03 \\ i think ok
Let's try to adjust the structure
17 27 17 \\ again ???
20 24 14 \\ ??? and i even tried to provide directly an int instead of a
transformed one so ???

ps: the output was tabbed well it doesn' show here don't know why
Last edited on

23 27 17 \\which aren't neither octal nor hexadecimal so what are and why?

But they are:
23dec == 27oct == 17hex

ps: the output was tabbed well it doesn' show here don't know why

Use the [output]program output here[/output] tags.
Last edited on
23 27 17 \\which aren't neither octal nor hexadecimal so what are and why?


Of course it is: 23.4356 as int == 23 , 27 octal == 23 dec , 17 hex == 23 dec
Topic archived. No new replies allowed.