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
Ok my problem is, in my dynamic array function I have an array that gives me the error below.
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <fstream>
using namespace std;
class Revenue
{
};
static int Track_Num_Divisions = 1;
static int Track_Quart_Revenue = 1;
void Program_loop()
{
{
string D;
string DN;
int N;
double TS;
double TC;
double P;
int arry;
cout << "how many Revenue tiers do you want?: "; cin >> arry;
Revenue* rev = new Revenue[arry];//dynamic array
for (int i = 0, Track_Num_Divisions = 1;Track_Num_Divisions, i < arry; i++,Track_Num_Divisions++ )
{
Revenue& rev = rev[i];// THIS IS THE ERROR <<<<
cout << " " << endl;
cout << "Revenue #"<<Track_Num_Divisions << endl;
cout << "===========" << endl;
cout << "<< Ok what is your division name?: " << endl; cin >> D;
string set_Division_name(D);
cout << "<< What is your division number?: " << endl; cin >> DN;
string set_Division_number(DN);
while (DN.size() != 4)
{
cout << "<< Sorry! Your Division Number cannot exceed or be short of 4. " << endl; cin >> DN;
}
delete[] rev;
}
Gives this error in function Dynamic_array
I think the problem lies in this code>> Revenue& rev =rev[i]:
Error 1 error C2676: binary '[' : 'Revenue' does not define this operator or a conversion to a type acceptable to the predefined operator
Error 2 IntelliSense: no operator "[]" matches these operands
operand types are: Revenue [ int ]
What should I do?
I am kinda new to this Website still learning the ropes of proper format.
This line is the problem:
Revenue& rev = *rev[i];
You're dereferencing the value returned by rev[i], but rev[i] is not a pointer or a class with an overloaded operator*. It's a Revenue&.
There's no need for derferencing anything here, just write it as:
Revenue& rev = rev[i];
Related
Edited -- See below for new info........ I'm new to programming - C++ - and was on Codingame where I ran in to a problem I can't find the answer for...
We were given two strings -- not char array which I expected and seems many of the answers I found on the web expected too -- and were expected to combine them in to one.
Example:
string1 = 01101
string2 = 01001
Answer = 01001
I have learned and understand how the answer is derived but I can't figure out the C++ code for it.
Any help would be great!
Thank you!
Thanks to nicomp I now know the correct terms are bitwise AND, not binary merge.
I was able to get a little further but still having issues getting the answer. I took the following code and got the output of 73.
#include <iostream>
#include <bitset>
#include <string>
using namespace std;
int main()
{
string str1 = "001101";
string str2 = "011001";
int a = stoi(str1);
cout << a << endl;
int b = stoi(str2);
cout << b << endl;
int c = a & b;
cout << c << endl;
return 0;
}
The result I should get is:
string str1 = "001101";
string str2 = "011001";
Answer = "001001";
or I would have settled for 9 since that is the non-binary value, not 73.
Can someone help me progress with this?
Thanks again!
int a = 0b01101
int b = 0b01001
int c = a & b;
You can convert a binary string to int like this:
std::string str = "1101";
int num = std::stoi("01010", 0, 2);
It's not 100% what I was looking for, but it does do what I wanted. The one part I would say isn't to my expectation is that I convert the numbers to decimal rather than leaving everything in binary. Regardless... this does take in two binary numbers and outputs one binary number after ANDing.
#include <iostream>
#include <bitset>
#include <string>
using namespace std;
int main()
{
string input1 = " ";
string input2 = " ";
cout << "Enter first binary number: " << endl;
cin >> input1;
cout << "Enter second binary number: " << endl;
cin >> input2;
int a = (int)bitset<64>(input1).to_ulong();
cout << a << endl;
int b = (int)bitset<64>(input2).to_ulong();
cout << b << endl;
int c = a & b;
cout << c << endl;
string output = bitset<8>(c).to_string();
cout << output << endl;
return 0;
}
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 3 years ago.
Improve this question
I'm trying to make a login page. When the program starts, the user has to type Guest and Password 1234 and he can edit his/her account. However, when I try to run it, it says:
Line 15 "Error incompatible types in assignment of 'const char[6]' to 'char[20]'
Line 16 "Error incompatible types in assignment of 'const char[5]' to 'char[20]'
I think it has to do with pointers but I am still a c++ newbie so I am having a hard time to understand pointers
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <string.h>
using namespace std;
const int LINE_LENGTH=20;
const int ID_LENGTH=8;
struct profile{char user[20];char password[20];double CGPA; int ID;};
int main(){
int count=0, i;
profile student[10];
student[0].user="Guest"; //Line 15
student[0].password="1234"; //Line 15
char signupName[20];
char signupPassword[20];
while (count==0)
{
cout << "#############################################\n";
cout << " Welcome to my program! \n";
cout << " Sign up to get started \n\n\n";
cout << " If you are starting, use username 'Guest' \n";
cout << " and password '1234' \n\n";
cout << "Username: ";
cin >> signupName;
cout << "Password: ";
cin >> signupPassword;
cout << "#############################################\n";
for (i=0;i<11; i++)
{
if(strcmp(signupName,student[i].user)==0 && strcmp(student[i].password,signupPassword)==0)
{
count++;
}
}
if(count==0)
{
system("cls");
cout<<"Your username and/or password is incorrect\n";
}
}
system("cls");
}
You need two minor changes to your code! First, as Francois Andrieux says, you can't assign char array strings with = ...
// student[0].user = "Guest";
// student[0].password = "1234";
strcpy(student[0].user, "Guest");
strcpy(student[0].password, "1234");
Second, your for loop runs once to often:
// for (i = 0; i < 11; i++)
for (i = 0; i < 10; i++) // Note: The last element in an array of 10 is x[9]!
This question already has answers here:
How to create an array when the size is a variable not a constant?
(6 answers)
Closed 5 years ago.
I've got some code that defines a variable called 'size' then uses it but for some reason it doesn't work ; the error is "Expression did not evaluate to a constant"
int main()
{
int size;
int userValue;
char input;
do
{
cout << "Enter an int for the size of the array:" << endl;
cin >> size;
int a[size];
cout << "Enter an integer between 1 and " << size << " to search for in the array." << endl;
cin >> userValue;
populateArray(a, size);
linearSearch(a, size, userValue);
binarySearch(a, size, userValue);
cout << "Press 'y' and enter to run again or just enter to quit";
cin.sync();
cin.get(input);
}while(input == 'y');
}
You can ignore the other functions as they are not called before the defining of my array, Ive tried setting size to a value, still doesn't work.
EDIT: forgot to mention
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
Why not use a vector?
std::vector<int> a(size);
Also in C++, you cannot define an array with a variable like that.
Alternative:
int* a = new int[size];
// Do stuff
delete [] a;
This question already has answers here:
Class method and variable with same name, compile error in C++ not in Java?
(7 answers)
Closed 7 years ago.
I'm new to c++ and I am creating a program to calculate the power of a number using recursion. After writing all the code, I'm getting an error:
Called object 'int' is not a function or a function pointer.
Here is my full code:
#include <iostream>
using namespace std;
int power(int, int);
int main(){
cout << "enter a number " << endl;
int no, power;
cin >> no;
cout << "enter a power" << endl;
cin >> power;
cout << "answer is " << power(no, power) << endl;
return 0;
}
int power(int number,int pow){
if (pow == 1){
return number;
}else {
return number* power(number, pow - 1);
}
}
Since I'm new I don't know what this means.
Thanks in advance
You first declare power as an integer:
int no, power;
But then try to call it as a function:
power(no, power)
They can't both have the same name. Rename either your integer or your function.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This script is supposed to read chars from keyboard, store them into arrays, and then output them:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void storeArraysintoStruct(char[], int);
int main()
{
char test[] ="";
int a = 0;
storeArraysintoStruct(test, a);
system("pause");
return 0;
}
void storeArraysintoStruct(char test[], int a)
{
int n;
cout << "Enter number of entries: " << endl;
cin >> n;
int i = 0;
for (i=0, i<n, i++)
{
cout << "Enter your character: " << endl;
cin.getline(test, n);
}
while (i < n)
{
cout << test[i] << endl;
i++;
}
}
Edit: fixed it:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void storeArraysintoStruct(char[], int);
int main()
{
char test[40] = "";
int a = 0;
storeArraysintoStruct(test, a);
system("pause");
return 0;
}
void storeArraysintoStruct(char test[], int a)
{
int n;
cout << "Enter number of entries: " << endl;
cin >> n;
int i;
for (i=0; i < n; i++)
{
cout << "Enter your character: " << endl;
cin >> test[i];
if (test[n-1])
{
cout << endl;
}
}
i =0;
while (i < n)
{
cout << test[i] << endl;
i++;
if(test[n-1])
{
cout << endl;
}
}
}
However, I am getting the errors Expected: primary expression before ")" and ";" before while. Any help will be greatly appreciated.
Edit: The script doesn't work as expected, for it doesn't output the stored characters. Any advice would be greatly appreciated.
The syntax error has already been pointed out in the comments. Also, as it has been mentioned, you never reset i after for loop, which prevents your while loop from running.
However, you have to also take in mind that this
char test[] = "";
allocates array test of only 1 character long. You cannot put more than one character of data into that array. In other words, your storeArraysintoStruct is sure to overrun the array and fall into undefined behavior territory.
In you want to preallocate a larger buffer for future use in storeArraysintoStruct, you have to specify the size explicitly. For example
char test[1000] = "";
will make test an array of 1000 characters. Of course, regardless of how large the array is, it is your responsibility to observe the size limit.
P.S. What is the point of that parameter a, if you never use it inside storeArraysintoStruct?