Trying to implement a function C++ - c++

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.

Related

c++ namespace class conflict

i have named this as next.h and included in main.cpp snippet
i have created two libraries one for the main function and the other just for having classes
i wanted to practise for OOPS so i suddenly thought of using namespaces to know their full potential but i am getting confused between why it isnt working as intended
#include<bits/stdc++.h>
namespace custom
{
class aries
{
public:
int data;
};
}
namespace custom2
{
class aries
{
public:
double data1;
};
}
#include<bits/stdc++.h>
#include "next.h"
using namespace std;
using namespace custom;
using namespace custom2;
int main()
{
custom2::aries a;
a.data1=5.000;
cout<<a.data1;
return 0;
}
The output for the following program is as follows:
5
...Program finished with exit code 0
Press ENTER to exit console.
My question is it should have been 5.000 but why it is int type and not a double type ?
Okay, just summing up the comments:
don't use the #include <bits/stdc++.h>. Instead, just include every header you need to use in your code separately. Reason: bad code readability and not every compiler supports this directive. More: Why should I not #include <bits/stdc++.h>?
also don't use the using namespace *name* command. For clarity: you can use namespaces, just don't use using, use *namespace name*:: instead, like std::cout. Reason: bad code readability, also you can mess up with names (like when you have function_name function and the namespace also has a function with the same name, then the compiler will not know which to use (or you get an error).
answer to your question: std::cout omits zeros while printing numbers so 5.00 will be just 5, but 5.278 will be 5.278.

Undefined reference to `WinMain' in C++ while exceuting on vectors

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.

Jupyter notebook error for C++ Kernel[cling]

I have installed cling kernel for using C++ in Jupiter notebook
but after implementing the code
#include <iostream>
using namespace std;
int main() {
int a;
a=9;
cout<<a;
return 0;
}
I am getting an error as ---> error: function definition is not allowed here
int main() {
In cling you don't write the whole program code. It's like a script language. You just write the lines that should be evaluated. Don't write the main function:
#include <iostream>
using namespace std;
int a;
a=9;
cout<<a;
You can also define functions in cling but then you are not allowed to write other code into the same cell.

Prototype Functions C++

I am having build errors when declaring prototype functions within Xcode. I am writing in C++. The script is pulled from my professor's lecture. Attached below is a picture of the build errors, along with the script itself.
Note: I only run into build issues when trying to declare prototype functions. It is as if Xcode is trying to pull the functions from a Library, and not recognizing it.
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>
#include <cctype>
#include <cstdlib>
using namespace std;
int calcSquare (int num) ;
int main ()
{
int num = 5;
int result;
result = calcSquare(num);
cout << "The Square of " << num << " is " << result << endl;
return 0;
}
Errors: https://farm3.staticflickr.com/2871/33406384892_68ee0843c7_b.jpg
The problem with your program is that you forgot to DEFINE the function prototype later in your source code.
Right now, you simply have a function prototype ( int calcSquare ), but you called that function anyways.
That throws you the linker exception as shown.
In other words, define the function somewhere in your source code to use the <cmath>, <cctype>, <cstdlib> modules and libraries.
After the function prototype, declare the function later:
int calcSquare (int num) {
...
}
I hope this helps! :)

Segmentation Fault C++ pointers [closed]

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.