Running Ubuntu 12.04, using the g++ compiler and have the essential-build installed. Currently trying to get a small little program to work the code is below:
#include "Muon.h"
#include "Electron.h"
#include "Photon.h"
#include <string>
using std::string;
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <ofstream>
using std::ofstream;
#include <vector>
using std::vector;
using namespace std;
int main(){
cout << "Let's create a Particle vector!" << endl;
Electron* a = new Electron(10.0,20.0,30.0);
cout << "Electron created" << endl;
Muon* b = new Muon(30.0,20.0,10.0);
cout << "Muon created" << endl;
Photon* c = new Photon(20.0,10.0,30.0);
cout << "Photon created" << endl;
vector<Particle*> particlesContainer;
particlesContainer.push_back(a);
particlesContainer.push_back(b);
particlesContainer.push_back(c);
int size = particlesContainer.size();
cout << "size is: " << size << endl;
ofstream file("Muon_Vector.data");
if (file.is_open()){
for (int i = 0; i < size; i++){
file << particlesContainer[0]->getType << endl;
}
}
else {cout << "Unable to open file" << endl;}
file.close();
return 0;
}
Upon trying to compile I receive the error:
"intParticles.cpp:15:20: fatal error: ofstream: No such file or directory
compilation terminated."
This is after typing: "g++ -Wall Particle.cpp intParticles.cpp -o run"
Any help as to what is going wrong would be grateful. I thought ofstream was part of the standard library?
ofsteam is class but not the header file. It is defined in 'fstream' file
Related
Im not sure how to include a "checker" to prevent incorrect input pls help :(
sorry, just started learning last night forgive me..
How should i do about this, without using if else statement or when should i be using that instead?
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int get_number;
char ch;
cout << "Hey! What is your number 1 OR 2?! :";
cin >> get_number;
switch (get_number)
{
case 1: cout << "You pressed 1/nPress anykey to continue!";
break;
case 2: cout << "You pressed 2/nPress anykey to continue!";
break;
default: cout << "Retry! 1 OR 2?! :";
cin >> get_number;
}
cin.ignore(1, '/n');
cout << "Press any key to continue";
cin.get(ch);
return 0;
}
So at my college we haven't covered do while loops yet so I wanted to try one out before we do but I am running into an issue with it, I want the program to carry out different functions based on the users input, A carries out an addition, B a subtraction and C exits the program.
The program I have wrote with a do while in it carries out these functions one after the other regardless of what the user has input and I am unsure how to get this working properly.
Any advice is much appreciated.
Couple things to note. You have user entering a character for a choice, so make userChoice a character. Notice how the do-while is used for input-validation in this case. I changed the extra while loops with semi-colons at end to condition statements. You can substitute the if and else ifs with switch, "menu driven program". Here is a working program that you want. If you want the do-while around the whole condition statement and not use it as input validation then have it around all the condition statements and check while it is not C.
#include <iostream>
#include <string>
using namespace std;
int main()
{
char userChoice;
int vaulue1;
int vaulue2;
int addtionValue;
int subtractionValue;
cout << "Choice A: will preform an addition" << endl;
cout << "Choice B: will preform a subtraction" << endl;
cout << "Choice C: will quit\n" << endl;
do
{
cout << "Enter your choice\n" << endl;
cin >> userChoice;
} while(userChoice != 'A' && userChoice != 'B' && userChoice != 'C');
if(userChoice == 'A')
{
cout << "Addition\n" << endl;
cout << "Please enter the first value you want to add\n" << endl;
cin >> vaulue1;
cout << "Please enter the second value you want to add\n" << endl;
cin >> vaulue2;
addtionValue = vaulue1 + vaulue2;
cout << "The addtion answer is " << addtionValue << endl;
}
else if(userChoice == 'B')
{
cout << "Subtraction\n" << endl;
cout << "Please enter the first value you want to subtract\n" << endl;
cin >> vaulue1;
cout << "Please enter the second value you want to subtract\n" << endl;
cin >> vaulue2;
subtractionValue = vaulue1 - vaulue2;
cout << "The subtract answer is " << subtractionValue << endl;
}
else if(userChoice == 'C')
{
cout << "Exit ";
return -1;
}
return 0;
}
you should use only one "while" - just first while is correct
other "whiles" are empty loop; clean them
it's better to use "switch - case"
while you enter A as input your method wants to give A another one
I wrote the following code in a file named test.cpp on godaddy web host:
#include <iostream>
using namespace std;
int main () {
cout << "Content-type:text/html\r\n\r\n";
cout << "<html>\n";
cout << "<head>\n";
cout << "<title>Hello World - First CGI Program</title>\n";
cout << "</head>\n";
cout << "<body>\n";
cout << "<h2>Hello World! This is my first CGI program</h2>\n";
cout << "</body>\n";
cout << "</html>\n";
return 0;
}
And I compile test.cpp on the godaddy host using "g++ test.cpp -o a.cgi".
Then I tried to access the cgi (type "www.XXX.com/a.cgi"), the error code 500 came out. I have no idea what went wrong.
Change the permissions of the file using file manager to make it executable
try www.xxx.com/cgi-bin/a.cgi
changing a.cgi to a.cpp and then check if it works
I'm very new to programming and I seem to have ran into a wall. I'm trying to write a simple code that reads scores from different Bowlers and then finding the average for each Bowler. My code seems to read the first line of scores and then stops. Any advice? Here's what I have
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int main ()
{
ifstream inFile;
ofstream outFile;
double score1, score2, score3;
double average;
string firstname;
string lastname;
inFile.open ("scores.txt");
outFile.open ("scoreavg.out");
ifstream ("scores.txt");
string content;
while (inFile >> content)
{
cout << content << ' ';
inFile >> firstname >> lastname ;
outFile << "Bowler name: " <<firstname << " " << lastname << endl;
inFile >> score1 >> score2 >> score3;
outFile << "scores: " << setw(4) << score1 << setw(4) << score2 << setw(4)<< score3 << endl;
average = (score1 + score2 + score3)/3;
outFile << "Average score: " << setw(4) << average<< endl;
inFile.close();
outFile.close();
return 0;
}
}
Short answer: the return 0; statement is inside the while loop.
You want your loop to do some reading and averaging for each record in the input. Once that's done, you then cleanup by closing the files. So the loop ends after the record is processed and before the cleanup.
Move the brace from after the return to before the first close.
I reduced my prog to the minimum just to show the pb: cmake does not complain, neither make (running Linux / libglew 1.5 / cmake 2.8.2). The program segfaults, and I have no clue why / which steps I should now make to solve the pb.
Source of render.cpp:
#include <GL/glew.h>
#include <GL/glut.h>
#include <GL/glext.h>
#ifndef WIN32
#include <GL/glx.h>
#endif
#include <iostream>
#include <assert.h>
using namespace std;
int main()
{
cout << "Before init" << endl;
GLenum err = glewInit();
cout << "After init" << endl;
if (err != GLEW_OK)
cout << "Initialization error" << endl;
else{
cout << "Successful init" << endl;
assert(glCreateShader);
GLuint f = glCreateShader(GL_FRAGMENT_SHADER);
}
}
The content of CMakeList is:
cmake_minimum_required (VERSION 2.6)
project (render)
IF(CMAKE_COMPILER_IS_GNUCC)
SET(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -Wall -std=c++0x")
ENDIF(CMAKE_COMPILER_IS_GNUCC)
# Some directory shortcuts
SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/conf)
INCLUDE(FindOpenGL REQUIRED)
INCLUDE(FindGLEW REQUIRED)
INCLUDE_DIRECTORIES(${OPENGL_INCLUDE_DIR})
INCLUDE_DIRECTORIES(${GLEW_INCLUDE_DIR})
message(${OPENGL_LIBRARIES})
message(${GLEW_INCLUDE_DIR})
message(${GLEW_LIBRARIES})
add_executable(render render.cpp)
target_link_libraries(render ${GLEW_LIBRARIES} ${OPENGL_LIBRARIES})
As mentioned, it compiles and segfaults at the call to glewInit(). I have no idea what to search for at this point..
Any help would be great,
Thanks
Might be useful to anyone, I solved it: using openGL, you need to create a context before being able to call glewInit(). This can be done using the SDL library (simplifies this task). Have a look at this tutorial.