Programming

I would like to know what command do I use when i wanna create a program that wil do what i want by pressing a letter after running program
ex:
*I press 'L'*
- Program shows 22 -


See: http://www.cplusplus.com/doc/tutorial/

Program reads input and stores it into variable.
IF the variable equals 'L'
THEN program writes 22 into output
I understand that, i know how to make program where a person enters something and the program outputs it. But i m curious if i can make a program where i dont need to enter anything. Just like when program is finished it displays " Press any key to exit" . I want by pressing a key,after running program, that i declared in program it shows what i told it to show. I hope u understand me.
no, we probably don't understand you.

at a guess:
you can put your inputs into a file and read the file
you can also put the inputs into a file and redirect the file into the executable and it will read that via cin AS IF you had typed it in.

I write programs all the time with no input or output; they quietly process some files for me.
Allright, is there a tutorial on how to put the inputs into a file and redirect the file into the executable?
Thanks btw.
If I have understood what you asked, perhaps you want to capture the so called “exit codes”. If so, the method to do that is machine dependent.

From the point of view of an executables, once it has terminated…, well, it has terminated :-)
Sorry for the tautology, but what I mean is it can’t help you any more: the operative system will just dump it from memory (unless of course it’s a daemon or terminate-and-stay-resident program, or however you call it, which might no more answer to commands, but be still in memory).

But programs can send back an ending code to the operative system when they terminate, and the operative system can catch it.

In a Microsoft environment the simplest method to catch those exit codes is probably by means of a ‘batch’ file, a script with extension .bat.

Provided you have a program like this:
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
#include <cctype>
#include <iostream>
#include <string>


char getFirstCharFromCin();


int main()
{
    return static_cast<int>(getFirstCharFromCin());
}


// Might return a warning about missing return value
char getFirstCharFromCin()
{
    std::cout << "Please enter a character included in the English alphabet: ";
    for(std::string line; std::getline(std::cin, line); /**/) {
        if(!std::isalpha(line.front())) {
            std::cout << "Dude, I need a character!\n"
                         "Please enter a character included in the English alphabet: ";
            continue;
        }
        return line.front();
    }
}


And you compile it into an executable named misterh.exe, you can work out a batch file like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@echo off

misterh.exe

if %ERRORLEVEL% equ 108 (
   echo It should be a lowercase '22'
   goto THEEND
)

if %ERRORLEVEL% equ  76 (
   echo 22
   goto THEEND
)

echo Unmanaged exit code.

:THEEND
echo.
echo Execution complete
echo.


The above script executes misterh.exe and evaluates its exit code.

In a Linux environment things would likely be easier and more flexible.
You don't need a tutorial, its extremely simple.

in both unix and dos you do it the same way.
1) make a text file with 1 line per input, or I believe whitespace delimited works as well; it may depend on if your program uses getline or cin or a mix.
2) in your command shell, type programname < file.txt (for a default unhappy unix console you need the dumb ./programname < file.txt -- its possible to make the ./ thing go away with an edit to .profile).

program will execute as if you had typed the data, but it reads it from the file instead. This is VERY USEFUL for testing your classroom/console programs, or for your no-gui stub/test programs that exercise your classes or whatever.

There exist macro/gaming/etc type programs that can fill out forms in gui programs and mash buttons etc to exercise those as well, but you are on your own for that.

try it..
make a text file with 1 word in it.
run this program and redirect the file into it.
1
2
3
4
5
6
7
8
9
int main ()
{
  string a;
  
  cin >> a;
  cout <<"you typed " << a << endl;
  return 0;
}

Last edited on
Thank you very much :D
glad to help. console tricks seem to be overlooked in the classroom lately, and sadly.. this stuff really, really helps coders.

just beware: the file redirect is SILENT. That is, you can't see what it 'types' to the program so if the inputs in the file are not correct (say you had 2 entries in the file and 3 getlines in the program) it will just sit there forever waiting on the file to type more inputs that it will, of course, never have and without some output in the code to say where it is, you won't know what its malfunction is.

you can also redirect output the other way
programname > outputfile.txt (overwrites/creates)
or
programname >> outputfile (creates/appends existing)
Topic archived. No new replies allowed.