I have to calculate the longest prefix string in the program. I am using c++ for this and I don't actually know extensively about vector and its functions.
The code is:
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
class Solution {
public:
void longestCommonPrefix(vector<string>& strs)
{
string ans;
strs.size(); //no of rows
strs.push_back("flower");
strs.push_back("flower");
string one=strs[0];
string two=strs[1];
int oneL=one.length();
int twoL=two.length();
int min=oneL<twoL?oneL:twoL;
for(int i=0;i<min;i++)
{
char temp=one[i];
if(temp==two[i] )
ans=ans+temp;
}
}
};
This displays an error of winMain and I do understand it must be related to the main function but the problem is I cannot put nain() function here else it displays an error again. Thus, I cannot put a main function and I if I don't I face an error.
Help me out stackmates.
Related
I started learning C++ a few weeks ago. Now I'm trying to program a kind of shop as a challenge. I've made it 2 or 3 times before, but always in one program. This time I tried to put some functions I wrote in it, so the main file wouldn't be that messed up again.
The problem I'm having is, when I'm trying to import a function, I get this error message:
E0413 There is no suitable conversion function from 'std::basic_ostream<char, std::char_traits<char>>' to 'int'.
Here's the code:
Mainfile:
#include <iostream>
#include "Benutzer.h"
using namespace std;
int main()
{
user;
}
Function:
#include <iostream>
using namespace std;
int user
{
cout << "So you're a user. What do you want to buy?"
}
I know it's not much code by now, but I was already testing.
As I can see, your program has an error in syntax.
Two things you need to consider here:
How to define a function:
return_type func_name(data_type args){
/// function body
}
How to call a function:
func_name(args);
I verified your code with little change on my system.
This is correct code:
main_file.cpp
#include <iostream>
#include "Benutzer.h"
using namespace std;
int main()
{
user();
}
Benutzer.h
#include <iostream>
using namespace std;
int user()
{
cout << "So you're a user. What do you want to buy?";
return 0;
}
This works.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am new to C++ programming but I know that pointers cause segmentation error. The problem is in the Readline() method when I am trying to read a sudoku but I cannot fix it. What am I missing?
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <algorithm>
#include "Sudoku.h"
using namespace std;
// Constructor
Sudoku::Sudoku(){
root=cells;
row=0;
row_ptr=&row;
}
void Sudoku::Readline(string s,int i) {
int lead;
for(int k=0;k<9;k++){
lead=(9*i)+k;
if (s[k]!=',') {
*(root+lead)=s[k];
} else {
*(root+lead)=0;
}
}
}
void Sudoku::MakeSudoku(string s){
//cout<<(*row_ptr)++<<' '<<s<<"\n";
Readline(s,(*row_ptr)++);
}
The class definition is
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class Sudoku{
public:
int cells[81];
int row;
int *root;
int *row_ptr;
public:
Sudoku();
void MakeSudoku(string s);
void Readline(string s,int i);
void PrintSudoku();
};
The main file is
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include "Sudoku.cpp"
using namespace std;
int main()
{
Sudoku sd;
// Input csv file containing sudoku
ifstream filen("sudoku.csv");
string s;
if(!filen.is_open()){
cout << "Error opening file";
} else{
while(!filen.eof()){
getline(filen,s);
sd.MakeSudoku(s);
}
}
filen.close();
//sd.PrintSudoku();
return 0;
}
Your code is no C++ code. Except file operations it is (bad styled) C code. You are using a plain array (cells), you even do an unnecessary copies of the array (root) and that pointer arithemtic is dangerous (as you are currently experiencing).
I think you should rewrite your code a bit which will solve your problem:
You should use descriptive variable names... k,s,i,etc. are hard to read
Use a two-dimensional array for 'cells'. Or even better a C++ container like a vector of vectors. The latter would check boundaries and you could get rid of your pointer arithmetics (which causes such faults when done the wrong way) and you could use plain indexes.
Use proper indentions and empty lines for block separation
Don't use magic numbers like "81" and "9". Create constants. Give them names! make them dependent from each other if they are dependent.
I am trying to create a lottery program within c++ and the issue i'm having is attempting to output all values of the Numbers array into a file, however when i run the code, the only thing that gets outputted is the first set of values i put in, however the program allows me to type in more than one set. (The program allows for up to 6 sets of data), however it only outputs one.
Here is all my code
LotteryData.cpp
LotteryData::LotteryData()
{
}
LotteryData::~LotteryData()
{
}
void LotteryData::PassInfo(int (&Numbers)[6][6], int &NumberofGames)
{
ofstream Numfile;
while(NumberofGames>0)
{
Numfile.open("Numbers.txt");
for (int j=0; j<6; j++)
{
Numfile << Numbers[NumberofGames][j];
}
NumberofGames = NumberofGames - 1;
Numfile.close();
}
}
Player.h
#pragma once
#include <iostream>
#include <fstream>
using namespace std;
class Player
{
private:
public:
Player();
~Player();
void Input();
int Numbers[6][6];
int NumberofGames;
};
main.cpp
#include <iostream>
#include "Lottery.h"
#include "Player.h"
#include "LotteryData.h"
using namespace std;
int main()
{
Player player;
Lottery random;
LotteryData data;
player.Input();
random.setRandomNumber();
data.PassInfo(player.Numbers, player.NumberofGames);
}
Im not exactly sure where the problem is coming from but i think it may be from one of the pointers although not entirely sure.
Any help on this problem would be much appreciated.
Cheers
Edit: I've Changed the code within the PassInfo function within LotteryData.cpp file as #ali suggested
Edit2: I've cut down on the code as to where I think the problem occurs but as all of the code compiles, Visual Studio 2012 doesnt point to any actual errors in the program
In the while loop in the PassInfo function you are not changing the value of NumberOfGames. Therefore in the for loop you are using the same row of your Numbers array. You need another for loop to change the index for the first index of the Numbers[][] array.
I am working on a project that asks the user to input a string, and then through get and set functions simply displays the string. However I am having issues actually having the user input the string and then pass those to the get and set functions. Here is my code:
This is my Main.cpp :
#include "stdafx.h"
#include <iostream>
#include "Laptop.h"
#include<string>
using namespace std;
int main()
{
Laptop Brand;
string i;
cout << "Enter your brand of laptop : ";
cin >> i;
Brand.setbrand (i);
return 0;
}
This is my Laptop.cpp :
#include "stdafx.h"
#include <iostream>
#include "Laptop.h"
#include <string>
using namespace std;
void Laptop::setbrand(string brand)
{
itsbrand = brand;
}
string Laptop::getbrand()
{
return itsbrand;
}
and this is my laptop.h :
#include<string>
class Laptop
{
private :
string itsbrand;
public :
void setbrand(string brand);
string getbrand();
};
In my laptop.cpp I have errors with setbrand and getbrand. They say that getbrand and setbrand are incompatible . I am pretty sure it has to do with I am passing a string through the parameters. Any ideas?
You have missed to include the correct namespace in the laptop.h file therefore the compiler cannot find any declared string class in the current (global) namespace. Just put, in the beginning of the file, using std::string;.
On a side note I'd avoid generic
using namespace std;
because it fights the purpose of having namespaces in the first place. It's usually better to specify exactly what kind of class you are using. Therefore:
using std::string;
is better.
The good fix here is to use std::string instead of string in the header file:
class Laptop
{
private :
std::string itsbrand;
public :
void setbrand(std::string brand);
std::string getbrand();
};
unlike the other files you do not have using namespace std. I would actually suggest just using std::string everywhere. It is safer and will save you from worse problems later on.
I'm running into an irritating problem where my program keeps crashing if I try to reference a private variable that I have created in one of my classes. I can't figure out where I am going wrong. Here is the class that calls the class that crashes:
#include <stack>
#include <fstream>
#include <ostream>
#include <cstdlib>
#include <string>
#include <set>
#include "schemeList.cpp"
using namespace std;
class dataLog
{
public:
stack<string> commands;
set<string> domain;
processor tokens;
int nextToken;
schemeList * s;
dataLog(stack<string> s, ofstream * out, processor p, int location)
{
commands = s;
tokens = p;
nextToken = location;
commands.push("<Query List>");
commands.push(":");
commands.push("Queries");
commands.push("<Rule List>");
commands.push(":");
commands.push("Rules");
commands.push("<Fact List>");
commands.push(":");
commands.push("Facts");
commands.push("<Scheme List>");
commands.push(":");
commands.push("Schemes");
checkNext();
}
void checkNext()
{
for(int i = 0; i < tokens.tags.size(); i++)
{
if(commands.top().compare(tokens.tags[i].getName())!=0)
{
if(commands.top().find("<")==0)
{
if(commands.top().compare("<Scheme List>")==0)
{
int output = (*s).process(i, tokens, domain); string hi = (*s).toString();
}
}
}
commands.pop();
}
}
};
This class creates an object of my SchemeList class, which is written out as follows:
#include "schemes.cpp"
#include <cstdlib>
#include <string>
#include <set>
using namespace std;
class schemeList
{
private:
string success;
public:
int process(int number, processor p, set<string> domain)
{
success = "HELLO";
return 13;
}
string toString()
{
return success;
}
};
As soon as I get to line 15 success = "HELLO";, the program crashes with the message
Unhandled exception at 0x00E48B66 in lab2.exe: 0xC0000005: Access violation reading
location 0xCCCCCCE4.
I am using Microsoft Visual Studio Express 2012 for Windows Desktop.
First off, the variable schemeList * dataLog::s is never initialized, so accessing it is undefined behavior, which leads to the crash. Most likely calling process on a dangling pointer and attempting to write into some memory you don't own.
Second, don't #include "schemeList.cpp". You're not supposed to include cpp files. Rather, separate declarations & implementations and include a header.
You have not initialized dataLog::s. When you call (*s).process(i, tokens, domain), you get undefined behavior.
Firstly, you're apparently including source code files in headers. This will likely break the one definition rule and go horribly wrong.
Secondly, 's' is not a very good name for a class member. It makes it almost impossible to find uses of it.
Thirdly, I can see nowhere in your code that initialises s. I can see where it gets referenced OK, but as it hasn't been initialised, the effect of dereferencing is undefined, and with luck will merely crash your program, which looks like what is happening.