What's wrong!! :/


Here is my code to distinguish between integers and numbers with decimal point. the table should display all numbers with their notes,
but here the table display only the last two numbers what to do?? :!

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
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{

	ifstream infile;
	infile.open("data.txt");
	string integerPart;
	string fractionPart;
	string Num[8];
	string value1,value2;

	cout << "The numbers from the file are: " << endl;
	for(int i=0;i<8;i++)
	{
		infile >> Num[i];
		cout << Num[i] << endl;
	}

	for(int i=0;i<8;i++)
	{
		if(Num[i].find('.') < Num[i].length())// a number with integral part and fractional part 
		{
			integerPart = Num[i].substr(0,Num[i].find('.'));
			fractionPart = Num[i].substr(Num[i].find('.'), Num[i].length());
			value1 = integerPart + fractionPart;
		}
		else // a number with integral part only
		{
			integerPart = Num[i].substr(0 , Num[i].length());
			value2 = integerPart;
		}
	}

    //display a table to distinguish between integers and float numbers.
	cout << endl;
	cout << "Number" << "    :    " << "Note" << endl;
	cout << "-----------------------------------------------------------------" << endl;
	for(int i=0;i<8;i++)
	{
		cout << value1 << "      :   " << "The number has both integral and fractional part" << endl;
		cout << value2 << "        :   " << "The number has only integral part" << endl;
	}
	cout << "------------------------------------------------------------------" << endl;

	infile.close();
	return 0;
}








Your first for loop writes the answers only into value1 and value2, overwriting the previous values with each iteration. Thus, after the loop is complete, value1 and value2 have the "last 2 values" as you put it.

Your output for loop just prints those same two values over and over again 8 times.

What you'll probably want to do is make just a single "value" array and store the answer for each number in that, then print out each of those elements at the end.

ahaaaa, thanks it works ^^.

here is my new code:

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
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{

	ifstream infile;
	infile.open("data.txt");
	string integerPart;
	string fractionPart;
	string Num[8];
	string value[8];

	cout << "The numbers from the file are: " << endl;
	for(int i=0;i<8;i++)
	{
		infile >> Num[i];
		cout << Num[i] << endl;
	}

	cout << endl;

	cout << "Number" << "   :    " << "Note" << endl;
	cout << "------------------------------------------------------------" << endl;

	for(int i=0;i<8;i++)
	{
		if(Num[i].find('.') < Num[i].length())// a number with integral part and fractional part 
		{
			integerPart = Num[i].substr(0,Num[i].find('.'));
			fractionPart = Num[i].substr(Num[i].find('.'), Num[i].length());
			value[i] = integerPart + fractionPart;
			cout << value[i] <<"   :   " << "The number has both integral and fractional part" << endl;
		}
		else // a number with integral part only
		{
			integerPart = Num[i].substr(0 , Num[i].length());
			value[i] = integerPart;
			cout << value[i] <<"     :   " << "The number has only integral part" << endl;
		}
	}

	cout << "-------------------------------------------------------------" << endl;
	cout << endl;
	

	infile.close();
	return 0;
}
Topic archived. No new replies allowed.