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.
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)
//* 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;
}
//* Kemal*//
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <vector>
int main() {
usingnamespace 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;
}
#include <iostream>
//#include <stdio.h> // <--- If needed should be "<cstdio>".
#include <fstream>
#include <vector>
int main()
{
usingnamespace 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.
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.
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 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.
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).