I want to execute in Code::blocks IDE program providing standard input.
Say
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double a;
double b;
cout << "Side one\n";
cin >> a;
cout << "Side two\n";
cin >> b;
cout << "Result :" << sqrt(a*a + b*b) << endl;
}
With arguments "a" and "b" , provided in file:
Say:
2 4
I could do that in bash, compiling and then:
"./my_compiled_program < ./myinput"
I simply want to have, file with argument (standard input) in code::blocks.
How can I do this?
PS: The "set program's arguments" unfortunately don't works
Thanks to #aleguna:
We should make file in folder where is the file we want to compile and execute:
And then add the < ./input to program arguments, where input is the name of file we placed next to source code.
Thank you #aleguna.
Related
i am trying to make the user pick a number thats leads to a another code in c++ how do i do that?
cout << "Choose a text: "
getline(????)
1:
code number one
2.
text number 2
Pick a number?
I would assume you are talking about inputing a integer.
getline mostly is used for a string and spaces between each words, or a word.
For example if the input is like "Happy thanks giving"
the code would be like:
#include <iostream>
#include <string> //to use getline(cin, variable_name)
using namespace std;
int main()
{
string sentenceWithSpace;
cout << "get input" << endl;
getline(cin,sentenceWithSpace);
cout << sentenceWithSpace << endl;
system("pause"); // include this line if you use VS
return 0;
}
if the user is just inputing a value like 1,2,3,4,5,6
#include <iostream>
using namespace std;
int main()
{
int thisIsJustAInteger;
cout << "get input" << endl;
cin >> thisIsJustAInteger;
cout << thisIsJustAInteger << endl;
system("pause"); // include this line if you use VS
return 0;
}
this is giving me a wicked headache and was hoping I could find some help. The program is supposed to read in a program of 19 integers, then output the smallest (2nd integer) and largest (5th integer) to the screen. However, all my results yield garbage.
#include iostream>
#include <fstream>
#include <cstdlib>
using std::ifstream;
using std::ofstream;
using std::cout;
using std::endl;
//the goal of this program is to read in numbers from a file, then output the
//highest number and the lowest number to the screen
int main() {
ifstream fileInput;
int nOne, nTwo, nThree, nFour, nFive, nSix, nSeven, nEight, nNine, nTen, //there are 19 numbers in the file
nEleven, nTwelve, nThirteen, nFourteen, nFifteen, nSixteen, nSeventeen,
nEighteen, nNineteen;
cout << "Opening File" << endl;
fileInput.open("Lab12A.txt"); //the file is opened
if (fileInput.fail())
{
cout << "Input file opening failed. \n"; //the fail check doesnt pop up, so the file has been opened.
exit(1);
}
fileInput >> nOne >> nTwo >> nThree >> nFour >> nFive >> nSix >> nSeven >> nEight
>> nNine >> nTen >> nEleven >> nTwelve >> nThirteen >> nFourteen >> nFifteen //this is where they should be extracted
>> nSixteen >> nSeventeen >> nEighteen >> nNineteen;
cout << "The highest number is " << nTwo << endl;
cout << "The lowest number is " << nFive << endl;
fileInput.close();
system("pause");
return 0;
}
I wished to add only a comment but since I can't do that, I leave it as an answer.
I have copied your file and created a text file to try to reproduce your issue. At first everything went well (No issue at all). But with comment from Daniel Schepler, I changed file encoding to UTF8-BOM (You can do that easily from Notepad++ Encoding menu) and tried again. I got same values you posted. I can't give more explanation to exactly how values are to be interpreted but I wish someone with more experience enlighten us here.
First I wanted to thank everyone who looked at and commented on this I greatly appreciate it, the issue was ultimately pinned down to needing a full path to the .txt file as opposed to the relative path I initially posted. For what ever reason, my compiler couldn't recognize the file without it. Seems like a silly mistake but I'm relatively new to this so those are sure to squeak by. Thanks again everyone!
You can use class std::vector pushing the values then sorting the container and finally print the second and fifth elements:
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
int main(){
std::ifstream in("test.txt");
std::vector<int> vecInt;
int value;
while(in >> value)
vecInt.push_back(value);
in.close();
std::sort(vecInt.begin(), vecInt.end());
// second value is at index 1 and fifth value is at index 4
for(auto i(0); i != vecInt.size(); ++i)
if(i == 1 || i == 4)
std::cout << vecInt[i] << std::endl;
std::cout << std::endl << std::endl;
return 0;
}
I am not sure about what you mean with "largest fifth integer".
#include <iostream>
#include <fstream>
using namespace std;
int run_t = 0;
char q_mos;
char i_pstring;
int main () {
cout << "Would you like to write to the temporary datapcku database?\nSelect Y/N\n";
cin >> q_mos;
if(q_mos = char(Y)){ //for some reason I am having time resolving the value of Y
while(run_t=0){
cout << "Running Input Operations.\n";
cout << "Please provide me with a Question so it can be achrived in the Active DB(Directory)\n";
cin >> i_pstring;
cout << "Please tell me the answer...\n";
cout << i_pstring;
}
run_t=1;
} else {
run_t=1;
cout << "Booting into main operations...\n";
}
cout << "At diagnostic Boot menu, prepare for diagnostic on system config orginaztional routines.\n";
ofstream binlib;
binlib.open ("datapcku.bin", ios::app | ios::binary );
binlib << "Writing this to a file.\n";
binlib.close();
while(1){}
return 0;
}
As is is apparent I wanted to use my run_t variable to control complete program maneuverability but I am having a time executing q_mos to cin input and I can not understand why the logic appears to be failing, as in the simple while loop following the q_mos comparison wont execute even one I get inside the block.
Do i need to convert q_mos to a string? And what could be affect my run_t variable while loop.
A couple of things are wrong.
In your if(q_mos = char(Y)) statement,
a) you are assigning, not comparing (use == instead of =). This also applies to your while loop.
b) replace char(Y) with 'Y', as you are passing it at the moment, Y is perceived to be a variable.
I need to create a separate cpp file to pull a function from, my book and power point slide don't go into much detail on this. Please help.
"Include at least one function in a separate .cpp file."
I understand this is the format, but i'm lost how to do it.
For each function argument,
the prototype must include the data type of each parameter inside its parentheses
the header must include a declaration for each parameter in its ()
void evenOrOdd(int); //prototype
void evenOrOdd(int num) //header
evenOrOdd(val); //call
#include <iostream>
#include <iomanip>
#include <fstream>
#include <limits>
using namespace std;
int main()
{
char again;// assigned variable for run again loop
do
{
int n; //n=number of computers
int conn;// conn= total of connections
cout <<"\t*********************************;
cout <<******************************\n";
cout << "\t*This program will display a list ;
cout << of how many possible network*\n";
cout << "\t*connections you could have;
cout << given any number of computers. *\n";
cout << "\t*********************************;
cout <<******************************\n";
cout << "\n";
cout << "Enter the maximum number of computers\n";
cout <<for which you would like to;
cout <<calculate total connections: ";
cin >> n;
//validate the input
while (!cin || n <2 )
{
//Explain the invalid input.
cin.clear();
cin.ignore(numeric_limits <streamsize> ::max(), '\n');
cout << "You must enter a number range from 2-10\n";
cout << "Please enter your selection again: \n";
cin >> n;
}
//Block that will open file, write to file, and display to the console
cout << "\n";
ofstream myFile;
myFile.open("totalComputers.txt");
cout << "Number of Computers\tTotal Connections\n";
myFile << "Number of Computers\tTotal Connections\n";
cout << "////////////////////////////////////////\n";
myFile << "////////////////////////////////////////\n";
for (int count=2; count <= n; count++)
{
conn = count * (count - 1) / 2;
cout << setw(10) << count << setw(24) << conn << endl;
myFile << setw(10) << count << setw(24) << conn << endl;
}
//This was implemented to close the file,
//and notify user that data write is complete.
myFile.close();
cout << "\n";
cout << "The data file has finished writing.\n";
//This will allow the user to run the program again or exit the program
cout << "\n";
cout << "Would you like to run the program again? y or n\n";
cin >> again;
} while (again == 'y');
cout << "Have a great day, please press any key to exit.\n";
return 0;
}
I'm not quite sure if I understood your question correctly. I'm assuming all you have to do is having evenOrOdd implemented in a different cpp-file. (Also I'm returning an int since I don't understand what the function would do if it returned void)
Consider this an (almost) minimal example of how to have multiple cpp files.
This example is constituted of three files: foo.h, foo.cpp and main.cpp
foo.h:
/*
* This file contains the declaration. Prototype
*/
int evenOrOdd(int); //prototype
foo.cpp (This is the "Include at least one function in a separate .cpp file."-file):
/*
* This file contains the implementation
*/
#include "foo.h"
//returns 0 if even and 1 if odd
int evenOrOdd(int n){
return n%2;
}
main.cpp:
#include "foo.h"
#include <iostream>
int main(){
int val = 5;
std::cout << evenOrOdd(val) << "\n";
return 0;
}
Then compile as follows:
First run
g++ -c foo.cpp
This produces the object-file foo.o.
Then you compile main an link foo.o into it with:
g++ main.cpp foo.o -o main
and run with
./main
which should output 1 since val is 5 and 5%2 is 1.
I'm not sure this is exactly what you needed, but I hope it gives you an idea of how to possibly solve your problem.
Note 1: I did not follow best practices (like for instance header guards) in this example in order to keep it more or less minimal.
Note 2: I implemented evenOrOdd in a cpp-file because I understood your task asks you to do so, however you could have implemented it in the foo.h directly and then compiling would have just been:
g++ main.cpp -o main
Some ref-material:
http://www.cplusplus.com/forum/articles/10627/
http://www.learncpp.com/cpp-tutorial/19-header-files/
Using G++ to compile multiple .cpp and .h files
When I run this program and input, for example, the number 7, the final cout command only works occasionally. Otherwise, the program exits successfully but the result is not printed. Why is this happening?
#include <iostream>
#include <cmath>
double treble(double);
int main()
{
using namespace std;
cout << "Enter a number:" << endl;
double numways;
cin >> numways;
numways = treble(numways);
cout << "Your number trebled is: " << numways << endl;
return 0;
}
double treble(double n)
{
return n * 3;
}
You should put using namespace std; outside of all function declarations, right under your #include directives. Also, when you say it's not printing, is it that the console is closing before displaying your result? In that case, I would advocate using a simple cin to "pause" the program. You can do it exactly as #Nihar says, though I might suggest using a string instead of an int so that it doesn't break if you accidentally type something other than an int.
Something like this:
#include <iostream>
#include <cmath>
using namespace std;
double treble(double);
int main(){
cout << "Enter a number:" << endl;
double numways;
cin >> numways;
numways = treble(numways);
cout << "Your number trebled is: " << numways << endl;
string foo;
cin >> foo;
return 0;
}
double treble(double n){
return n * 3;
}
try with this => put
int temp;
cin>>temp;
before return 0; to pause the program, because the execution finished (successfully) before the last output could be written to the console.