Reputation: 11
I am trying to run a shell script I made that would run a cpp file named cs216PA1.cpp. The only problem is that when I go to run it it stops at line 10 and says that there is no such file or directory. This is the line that calls the cpp file. Should I be doing something different to call it? Let me know what you guys think, because this is stumping me.
The shell script:
#!/bin/bash
echo "Student Information Search..."
echo "Enter <Q or q> to quit the program."
echo "Enter any other key to continue..."
read usr_option
while [ "$usr_option" != "Q" && "$usr_option" != "q" ]
do
./cs216PA1
echo "Student Information Search..."
echo "Enter <Q or q> to quit the program."
echo "Enter any other key to continue..."
done
echo "Thank you for using the program!"
The cpp file:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string id;
cout<<"Please type the number of a student"<<endl;
cin>>id;
string name1[100];
string name2[100];
string courses[100];
std::ifstream infile1("sec1students.txt");
//infile1.open(sec1students.txt);
if(!infile1)
{
cout<<"sec1students.txt file cannot be opened!";
exit(0);
}
for (int num=0; infile1.good() && num<100;num++)
{
infile1>>name1[num];
}
infile1.close();
for (int j=0; j<100;j++)
{
if (name1[j] == id)
{
cout<<"Student number: "<<name1[j]<<endl;
cout<<"Name: "<<name1[j+2]<<", "<<name1[j+1]<<endl;
cout<<"Section: day-time"<<endl;
}
}
std::ifstream infile2("sec2students.txt");
//infile2.open(sec2students.txt);
if(!infile2)
{
cout<<"sec2students.txt file cannot be opened!";
exit(0);
}
for (int num=0; infile2.good() && num<100; num++)
{
infile2>>name2[num];
}
infile2.close();
for (int j=0; j<100;j++)
{
if (name2[j] == id)
{
cout<<"Student number: "<<name2[j]<<endl;
cout<<"Name: "<<name2[j+2]<<", "<<name2[j+1]<<endl;
cout<<"Section: evening"<<endl;
}
}
std::ifstream infile3("studentcourses.txt");
//infile3.open("studentcourses.txt");
if(!infile3)
{
cout<<"studentcourses.txt file cannot be opened!";
exit(0);
}
for (int num=0; infile3.good() && num<100; num++)
{
infile3>>courses[num];
}
infile3.close();
int numb=6000000;
for (int j=0; j<100;j++)
{
if (courses[j] == id)
{
cout<<courses[j+1]<<endl;
numb=j;
break;
}
}
if (numb==6000000)
{
cout<<"is not taking a course"<<endl;
}
}
Upvotes: 0
Views: 57
Reputation: 569
You can't run a .cpp file. You feed it to a compiler which gives you an executable binary. Assuming you have g++, you'd do something like:
g++ cs216PA1.cpp
That will make a binary called "a.out". To give it the name "cs216PA1", which I suspect you want:
g++ cs216PA1.cpp -o cs216PA1
If you do the above (with the -o), your bash script should work assuming everything else is fine.
If you're using clang, you can just replace "g++" with "clang++".
Upvotes: 1