Array

Hello,

I want to input only 4(numbers)from a .txt file
like Number of Plants: 4


Now where I have to change for this.
please let me know

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
#include<fstream>
using namespace std;

int main (void)
{
	ifstream inputFile;
	inputFile.open("flp.txt");
	if(inputFile.is_open())
	{
		int n;
		
		inputFile >> n;
		cout << n << endl;
	
	}
}
1
2
3
4
5
6
7
8
9
10
	if(inputFile.is_open())
	{
		int n[4];
		for(int i=0; i<4; i++)
		{
			inputFile >> n[i];
			cout << n[i] << " ";
		}
	
	}
Thank you anup
but my code is wrong
I wanted to input (only number) 4 after a big string
Example:
"Number of Plants: 4"
this line wrote in flp.txt file. I have to input only "4" which is stay end of this line.

How to do this please let me know.

Thanks
string s = "number";

s+= "4";

cout << s; // number4
No I want to input single number which is "4" after this line "Number of plants: 4"
what is the content of input file?
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <cctype>
using namespace std;

int main()
{
	string s ="hello 7";
	
	if(isdigit(s[6]))
		cout <<"ok";	
}
Probably shuvoshill was looking for something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cctype>

using namespace std;

int main()
{
    string s = "hello 7";

    for(unsigned int i=0; i<s.length(); i++) {
        if(isdigit(s[i])) {
            cout << "The digit is " << s[i]-'0' << endl;
        }
    }

    return 0;
}


Note: it only works with one-digit numbers, but it can be extended...
Last edited on
Thanks anup and minomic

I wanted to say that I have a .txt file and I want to input some information like this way
Plants 4
WareHouse 5
Demand 15 18 14 20
Capacity 20 22 17 19 18
Transport Cost 4000, 2000, 3000, 2500, 4500
2500, 2600, 3400, 3000, 4000
1200, 1800, 2600, 4100, 3000
2200, 2600, 3100, 3700, 3200
I need those information using different different array after that I need to calculate those data.
I hope you can understand.....
Ok, so in this case you want an array containing 4, then another array containing 5, then another one containing 15-18-14-20, etc. Right?
yes you are right after that I'll calculate using those array
Thank you for understanding me.
Topic archived. No new replies allowed.