What is the best way to convert an int to a char from a batch file(all numbers are ints) without using an array. C++
I'm not sure I understand the question. what do you mean 'convert an int to a char from a batch file'? Do you mean how to do it in a batch file? then how is it a C++ question?
Say I am getting a bunch of int from a batch file. So I have no idea what number I will be receiving. How would I convert the incoming numbers to different char as an output. For instance input for batch file is 3 output *.
What I am really asking is how would I convert a int to a char in c++ without using an array.
Ok, well char is limited to one byte, you have 256 possibilities to different 'chars' and they are not all visible characters as well.. so you can't convert any possible integer into a different singular char.
Can you be more specific about the possible integers and the desired conversion?
3 inputs say 4 6 -8 then each need to go to a specific character 4 = * 6 = $ -8= # Then it is suppose to print out a triangle with the amount I entered but I know I can do this with decrement with a for loop. so for instance for should look like this: **** *** ** *
Ok, so can you pick the inputs or the characters after the conversion or is it given to you?
inputs are completely random from a batch file but they all are ints that are coming in from the input.
What happen if I give an input of 10 in your example above? is 10 a valid input as well?
if 10 is the first number in the batch file it would go like this 10 10 = * ******** ******* ****** ***** **** *** ** *
I set my while loop to only take in 3 inputs from the batch file from them each are giving a specific number character *, #, or $ ( I was going to do each number threw a for loop). Then each of the 3 inputs will print out three different triangles with those characters.
Ok so you just have to give them different numbers based on order? then the value doesn't really matter, right? just the order?
yes, whatever the first 3 numbers of the batch file I can use. Then make three different triangles out of them. which I know I can do by making for loops. My real issue is getting this random number and converting it to one of those characters *,$, or #.
and would I have to convert before or once it is in the for loop to get the proper output of the triangles shown above.
But here is the thing.. I don't see why do you want to convert the random number. it seems like the value only control the size of the triangle. I would have done something like this: ```cpp void make_triangle(char c, int size) { // ... } int main(int argc, char **argv) { for(int i = 0; i < 3; ++i) { int r = ...; //Get random number here make_triangle('!' + i, r); } return 0; } ```
We are starting functions next week, So I do not want to jump ahead on the chapter. Could I use multiple for loops to get the number as a operator to another for loop to output the triangle for the characters?
You don't really need a function here, it just makes it a little more clear: ```cpp int main(int argc, char **argv) { for(int i = 0; i < 3; ++i) { int r = ...; //Get random number here char c = '!' + i; // Make triangle here with size of 'r' using character 'c' } return 0; } ```
int main()//I am trying to avoid using paramater since we are using that next week. { while(cin) //the random input from the batch file { for(i=0;i<3;i++) { } I am going to have to get back to you once I have more. I am trying to think how I would proceed from here. I still need to get the 3 random inputs then making them output the traingle.
Wait, when you say 'batch' do you mean input from command prompt? because I see you want to use `cin` here
Well it is through the schools linux secure shell account. So it could be thought of similar I would think. Say if you made an input file that held whatever you like in it and you want to get those numbers from it. That essentially what I am doing.
I'm think I'm getting confused because I do not completely understand what are you supposed to do.. Every time it seems like there is a new piece coming heh Ok, so if you work with some linux shell, ok. What is the exact command you are supposed to execute? I see you are supposed to build a c++ program, but how are you supposed to pass the input to it? Does the program prompt for input? do you give it as command line arguments like `./myprogram 5 3 10`? Are you suppose to read it from a file? and if you do, are you supposed to commit the reading using the shell or C++? As you can see I get more questions as we go lol
sorry, I am still new to this concept. basically the C++ source file is reading the input from the batch file. So the while(cin) is getting all the inputs which I know are all ints because i created input.cpp file that it reads once I run the c++ source file.
Lol don't be sorry, that's ok. I just hope I'm not tiring you with all those questions Ok, so is input.cpp supposed to be the C++ file? or the file holding the numbers that your program should read?
The input.cpp file is holding the numbers ( 1 2 5 6 8 3 0 -5 -8...etc). My file I am creating exercise5.cpp is reading the input (input.cpp) to get the numbers used in the main program exercise5.cpp. The reason I used while(cin).
Ok, that gets a little more clear. but first why is input.cpp a .cpp file? it doesn't seem to have anything to do with C++ at all, it's just a text file being read by your program. Second, `cin` is used to read from standard input, not files. If you want you can use shell commands to pass the file's content to your program's standard input, but your program doesn't read the file on its own this way.
Right the person who is grading my assignment will have a input.cpp from a text file to read to my program. It just they want it. It a standard input, I might have been saying it wrong. yes that would be correct we compile the main file exercise5 then use ./a.out < input.cpp so it reads the standard file input.cpp
Ah, I see that makes sense. so the way you execute your program will be `./a.out < input.cpp` and you want to use `cin` to get 3 numbers (or maybe more?) and use a different character for each one to make the triangle?
yes, that is correct.
Ok, but it is still essential to know. is it going to be exactly 3 numbers? could it be more? how much more?
Sorry I went over the assignment again. It says as many values that it reads in the standard file. I read it wrong basically for every input comes in it draws a triangle for the base and height is the integer for example: 3 *** ** * So each number that comes in it outputs the triangle of whatever number it is.
Ye I thought so, so if you get 3 and 6 you'll make two triangles, one in size of 3 and one in size of 6, right? but we still have to deal with the 'different characters', and for that we have to know how many numbers are we supposed to get?
yes, and that is my main issue from the beginning is how would these number go into the characters. How many numbers come in is unknown and completely random. I figure I could try to say the amount coming is not equal to the a sentinel value that counts the number of times it comes in.
but what if you get 1000 numbers? we don't have 1000 different characters... we only have 128 standard characters in ASCII and about half of them are not even visible... It doesn't make sense that you can get unlimited number of integers..
I think I can reuse characters as long as I am drawing the proper right triangle for the integers
Ok, so let's say we iterate through 10 different characters over and over again, ok?
Each valid data set will consist of an integer greater than zero and a character. This what it says for the first line of my assignment so this I assume is correct we can reuse characters as long it is one integer to one character.
Is it possible that you can show me the exact text of the assignment?
A data file contains several data sets. Each valid data set will consist of an integer greater than zero and a character. These values will be used to draw a right triangle. The integer represents the length of the base and height of the triangle. The character is the symbol used to draw the triangle. The program should read a data set and if it is valid, draw a triangle using the input values. The triangle must be oriented as shown in the examples below. For each drawing, display the input values used. The program should continue processing input until an invalid data set is read.
It seems like you're supposed to get the character for each triangle as part of the data in the file Something like ``` 5 # 10 * 3 # 8 @ ``` and so on
okay yes that is correct, that would have severely messed up my program. Now how would I get the integer and the character from the date file to do each of the triangles? I can still do a while loop to watch for a sentinal value. I am just wondering If I would have to use two for loops for the character and one for the number.
Hold on let me test it sec
How about this: ```cpp #include <iostream> using std::cin; using std::cout; using std::endl; int main(int argc, char **argv) { int size; char c; while( cin >> size >> c ) { // Make triangle here with size of 'r' using character 'c' } if( !cin.eof() ) { cout << "Invalid input" << endl; return 1; } return 0; } ```
We havent done parameters yet but I get the basic idea. what does !cin.eof() mean? can you explain to me the first while(cin>> size>> c) is doing?
What do you mean parameters? `cin >> size >> c` gets two inputs (separated by whitespaces), the first is a number that gets into `size` the second a character that gets in to `c` So for example, if your input line is: `4 *` Then you'll get 4 into `size` and * into `c`
sorry I mean we have not done arguments into methods yet.
the `cin.eof()` checks if you have finished the whole input given to you. If `cin` hasn't reached the end of the input after the loop then it means that `cin` failed to fetch input not because its over, but because there was an error parsing it. So I added a check there to see if all the input was parsed or not.
oh okay thanks, I will develop my program with the advice you gave me. If I have anymore questions about my code when I develop it I will post it here. thanks
Ok, I made here a code for this so I can show you in case you get in troubles good luck =)
Point of interest, you can use a char as a number, so it is pretty easy to convert it to an int.
Join our real-time social learning platform and learn together with your friends!