reading as numbers and saving as colors

Hi,
i need to develop a program that writes a color to the screen and each line of another file according to the numbers read from a file. (The file name to be read must be read.txt, write the file name to be written.txt) 0: black 1: blue 2: red 3:green 4:white

If you ask me why you can't do it because I'm studying at the Faculty of Fine Arts.They forced me this lesson (General Programming) and I don't understand anything.I just need to paint.

Thanks in advance for your help
this is another question but i did it.This is question 1and above question question 2
Write the program that writes the color on the screen according to an integer requested from the user. (According to (mode 5), it should write 0: black 1: blue 2: red 3: green 4: white for its value) (extra 5 points will be added for those who write with switch) (add the source file)

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
//* Kemal*//
#include <stdio.h>

int main() {
	int color;
	printf(" enter a number between (0-4) \n");
	scanf("%d",&color);
	printf("\n");
	switch (color){
		case 0 :
			printf("Black");
			break;
		case 1 :
			printf("Blue");
			break;
		case 2 :
			printf("Red");
			break;
		case 3 :
			printf("Green");
			break;
		case 4 :
			printf("White");
			break;
		default :
			printf("Wrong!!");
	}
    return 0;
}

.
Hello kmlz,

That is a good start. Now all you need to do is modify it to read from a file instead of the keyboard.

You will need a file stream for input and 1 for output.

Andy
Maybe ?
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
//* Kemal*//
#include <iostream>
#include <stdio.h>
#include <fstream>										
#include <vector>
int main() {
	using namespace std;
	ifstream data("read.txt");						
	ofstream outfile("written.txt");						
	if (!data.is_open())							
	return (cout << "The file could not be opened\n"), 1;
	vector<double> A;									
	for (double v ; data >> v; A.push_back(v));			
	int number ; 
	
	

	int color;
	cout << "enter a number between (0-4) \n: ";
	cin >> number;
	printf("\n");
	switch (number){
		case 0 :
			printf("Black");
			break;
		case 1 :
			printf("Blue");
			break;
		case 2 :
			printf("Red");
			break;
		case 3 :
			printf("Green");
			break;
		case 4 :
			printf("White");
			break;
		default :
			printf("Wrong!!");
			
		outfile << ?????? << '\n';
	}
    return 0;
}
Hello kmlz,

Better, but I would suggest not mixing C and C++ code.

I am having problems getting this to run, so it is untested:
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
#include <iostream>
//#include <stdio.h>  // <--- If needed should be "<cstdio>".
#include <fstream>										
#include <vector>

int main()
{
    using namespace std;

    ifstream inFile("read.txt");
    ofstream outFile("written.txt");

    if (!inFile)  // <--- This is all you need.
        return (cout << "The file could not be opened\n"), 1;

    if (!outFile)
        return (cout << "The file could not be opened\n"), 1;

    vector<double> A;

    for (double v; inFile >> v; A.push_back(v));

    int number;
    int color;

    cout << "enter a number between (0-4) \n: ";
    cin >> number;

    //printf("\n");  // <--- Should use "cout"

    switch (number)
    {
        case 0:
            cout << "Black";
            break;
        case 1:
            cout << "Blue";
            break;
        case 2:
            cout << "Red";
            break;
        case 3:
            cout << "Green";
            break;
        case 4:
            cout << "White";
            break;
        default:
            cout << "Wrong!!";

            //outFile << ? ? ? ? ? ? << '\n';
    }

    return 0;
}

As to the file stream names I like "inFile" and "outFile". I find it easier to follow in the code. You are free to use whatever name you like.

Not sure what you are doing with line 51, but it will not compile that way.

For some reason I am having trouble with my VS 2017 building the ".exe" file which is why it is untested, but I believe it should work.

Note: it is best to check that both an input file stream and an output file stream is open and ready to use. Do not count on an "ofstream" creating a file if it does not exist.

Andy
files are exist :).
read.txt files have to be data.
I write 0 1 0 4 0 3 2 to the read.txt file. I save and close. I'm running the program. Read.txt contents deleted and empty in written.txt?

Teacher told to us:
Develop a program that writes a color to the screen and each line of another file according to the numbers read from a file. :/

I think it will write 0-4 numbers in the read.txt file.
0
1
1
4
etc....

When I run the program it will save in the written.txt file as below.
0: Black
1: Blue
1: Blue
0: Black
4: White
... etc.
Hello kmlz,

Sorry for the delay.

After looking at your program I realized that you are creating a vector that you never use. So unless you have a need for a vector that I do not see yet you do not need it.

To make better use of what you have and what you need I changed the for loop and case statements to this:
1
2
3
4
5
6
7
8
9
10
11
12
//vector<int> A;  // <--- Never used. Or needed.

for (int number; inFile >> number;)
{
    std::cout << '\n';

    switch (number)
    {
        case 0:
            cout << "Black";
            outFile << number << ": Black\n";  // <--- Added.
            break;


This produced a screen output of:

Black
Blue
Red
Green
White
Black
Red


And a file of:
0: Black
1: Blue
2: Red
3: Green
4: White
0: Black
2: Red


I believe that is what you are looking for.

Andy
Andy Thank you so much. is this true ? I think I did it wrong because it only saves the first line :)

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
#include <iostream>
#include <fstream>										
#include <vector>

int main()
{
    using namespace std;

    ifstream inFile("read.txt");
    ofstream outFile("written.txt");

    if (!inFile)  // <--- This is all you need.
        return (cout << "The file could not be opened\n"), 1;

    if (!outFile)
        return (cout << "The file could not be opened\n"), 1;



for (int number; inFile >> number;)
{
    std::cout << '\n';

    switch (number)
    {
        case 0:
            cout << "Black";
            outFile << number << ": Black\n";  // <--- Added.
            break;
        case 1:
            cout << "Blue";
            outFile << number << ": Blue\n";  // <--- Added.
            break;
        case 2:
            cout << "Red";
            outFile << number << ": Red\n";  // <--- Added.
            break;
        case 3:
            cout << "Green";
            outFile << number << ": Green\n";  // <--- Added.
            break;
        case 4:
            cout << "White";
            outFile << number << ": White\n";  // <--- Added.
            break;

    }

    return 0;
}
}
Hello kmlz,

Much better.

I would still keep the default case just in case there is something wrong with the input file. Also it is good practice for dealing with problems as you will learn in the future.

But if yo accept that the input file can never be wrong then you can do without the default case.

Although I would change the error message to say that there is something wrong with the input and not just say "wrong!".

Using a for loop to read the file is fine, but it is more common to use a while loop. This does not need to be changed unless you are interested in learning a different way.

Andy
You don't need the switch statement. Just use an array of colour names:

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

const char* const names[5] {"Black", "Blue", "Red", "Green", "White"};

int main()
{
	ifstream inFile("read.txt");
	ofstream outFile("written.txt");

	if (!inFile || !outFile)
		return (cout << "The files could not be opened\n"), 1;

	for (int number; inFile >> number; )
		if (number >= 0 && number <= 4) {
			cout << names[number] << '\n';
			outFile << number << ": " << names[number] << '\n';
		} else
			cout << "Unknown number " << number << '\n';
}

Last edited on
Thank you seeplus,
it is working but it says
[Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11
6.Line
That is telling you that you tried to build the program with outdated settings. You should use the 17 setting if you have it, at the very least (these are years from 2000, so 11 is 2011 c++, 17 is 2017 and the current 'standard' though there is a 20 that is not heavily in use yet).

basically, do what it said, add those to your compile command or change your IDE settings or something (tell us what you have, or google at it, to see how to set this for your specific setup).
Topic archived. No new replies allowed.