I've just now come across an error using arrays that seems odd, I've searched the web but it would appear this is typically something people run into when they are dealing with multidimensional arrays. The error occurs when I attempt to call a function that uses an array as a parameter. Here is the code:
#include <iostream>
#include <string>
#include <iomanip>
#include "header.h"
#include <fstream>
using namespace std;
int main(){
string make, model, licensePlate, address, name, phoneNumber;
int year, messageCode;
char choice;
bool ready[10];
string phoneNumber[10], name[10], address[10], licensePlate[10], make[10], model[10];
int year[10];
initializeArray(year[]);
The error occurs within the brackets of year[], expected an expression. Thanks in advance!
I apologize for my poor code!
Having both variables of int and an array with the same name was a terrible idea, as you do not use the brackets to pass an array. I removed the brackets and the int year variable.
Here is the revised work:
bool ready[10];
string phoneNumber[10], name[10], address[10], licensePlate[10], make[10], model[10];
int year[10];
initializeArray(year);
Related
#include <iostream>
#include <time.h>
#include <string.h>
#include <stdio.h>
int main()
{
string msg;
printf("Enter the message that you wish to display as scroller: ");
getline(cin,msg);
msg=msg+". ";
int x=0;
while(1)
{
Scroll(msg);
wait(100);
system("cls");
x++;
}
cin.get();
return 0;
}
I Have this C code and all strings in the file say 'identifier "string" is undefined'. I tried including <string> instead of <string.h> but it didn't work. Why is it not working?
Add
using namespace std;
After includes (but before main). Or, better, use notion of:
std::string // instead of string
Update: I missed the point of this being C-question. I will leave this answer, but for the sake of formality, use it if you came from Google and you are working with C++.
This is C++ code, not C.
The compiler is probably getting confused because it cannot parse it, so then it finds C-like code and all identifiers do not exist.
The includes should be:
#include <iostream>
#include <ctime>
#include <string>
#include <cstdio>
You are also missing a:
using namespace std;
Plus the definitions for Scroll and wait etc.
Currently going thru a c++ course.
I had to create a word cipher using the strings: alphabet and key.
to cipher an inputted word with less code as possible I created this solution that gives the error:
no matching function for call to std::basic_string<char>::find(std::string&, int&, int)
I don't know how to solve it, neither do I know if my idea would work at all, would LOVE some help.
Thanks for your attention :)
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main() {
string alphabet {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
string key {"XZNLWEBGJHQDYVTKFUOMPCIASRxznlwebgjhqdyvtkfuompciasr"};
string word_to_encrypt {};
getline (cin,word_to_encrypt);
for (int i=0;i<word_to_encrypt.size;i++){
word_to_encrypt.replace (i,1,key,(alphabet.find(word_to_encrypt,i,1)),1);
}
cout<< word_to_encrypt;
}
Two problems:
First size is a function and not a variable. Therefore you need size().
Secondly std::string::find() has no overload which takes a std::string and two ints: https://en.cppreference.com/w/cpp/string/basic_string/find , but you can use the overload which takes a CharT instead by adding .c_str() or .data().
This compiles at least:
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main() {
string alphabet {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
string key {"XZNLWEBGJHQDYVTKFUOMPCIASRxznlwebgjhqdyvtkfuompciasr"};
string word_to_encrypt {};
getline (cin,word_to_encrypt);
for (int i=0;i<word_to_encrypt.size();i++){
word_to_encrypt.replace(i, 1, key, (
alphabet.find(word_to_encrypt.c_str(), i, 1)),1);
}
cout<< word_to_encrypt;
}
I keep getting this error (it's a really long one but I think the most important part is this):
main.cpp:9:30: note: mismatched types 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>' and 'const char [2]'
While compiling this bit of code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string x = getline(cin, " ");
return 0;
}
The lines in the error won't match with the ones in the code I brought up here because I don't know how to make a new line whilst writing code in the Stack Overflow editor; I'm new here ;) Anyways, the error points to the line with the declaration of string x.
Basically what I want this code to do is to get a line from the user until he/she hits space. Maybe I'm doing something wrong from the beginning, so I'm open for suggestions of fixing this problem. (I'm not very experienced in C++, it's just that my teacher required to complete a task using this language.) Thanks,
Anthony
The second parameter of std::getline() is a reference to a std::string variable that accepts the read data. The string is not outputted in the function's return value.
Also, std::getline() does not accept a string for the delimiter. It takes only a single character.
Try this instead:
#include <iostream>
#include <string>
using namespace std;
int main() {
string x;
getline(cin, x, ' ');
return 0;
}
I apologize for asking a question that should have a simple solution, but it's driving me nuts. I've checked for all the common errors: namespace std, spelling, include vector, etc. Below is the abbreviated code for my video.h file.
#include <iostream>
#include <string>
#include <new>
#include <vector>
using namespace std;
class Video
{
public:
Video(string, string, string, float, int);
vector<Video*> video_ptrs;
void print();
};
And here is the code for my main.cpp
#include "video.h"
using namespace std;
int main()
{
...
Video* temp_here = new Video(title, url, comment, length, rating);
video_ptrs.push_back(temp_here);
return 0;
}
The error that returns says, "'video_ptrs' was not declared in this scope." Thank you ahead of time for any help given.
video_ptrs is a member of Video, call it with the object you just created:
Video* temp_here = new Video(title, url, comment, length, rating);
temp_here->video_ptrs.push_back(temp_here);
This adds a pointer temp_here to the vector of that same object though, I'm not sure if this is intended.
video_ptrs is indeed not declared in main. You shall use
temp_here->video_ptrs...
I am confused about the use of #include <string> at the start of a program. For example, in the code below, I don't use #include <string> but the function will still print out the string "Johnny's favorite number is" when it is run.
#include <iostream>
using namespace std;
void printVariable(int number){
cout << "Johnny's favorite number is" << number << endl
}
However, in this code below, it does contain #include <string>.
#include <iostream>
#include <string>
using namespace std;
class Var{
public:
void setName(string x){
name = x;
}
string getName(){
return name;
}
private:
string name;
};
int main(){
Var Classy;
Classy.setName("Johnny Bravo");
cout << Classy.getName() << endl;
return 0;
}
Do I only use #include <string> if a variable represents a string?
Do I only use #include <string> if a variable represents a string?
Yes.
Use #include <string> when you use a variable that has type std::string.
The code "text here", contrary to intuition, is not a std::string; it is a string literal, and a C-style string, and a const char[10] convertible to const char*. Welcome to C++ with its legacy oddities.
Your question arises from the fact that you know that something like "aabcd" is a string literal. So, its type should be string. Well, that's not quite true.
C++ has a lot of features from C. Including data types. So, that is a pointer to char (char*), not a string(an instance of the string class). You can create an instance of the string class from a char* (including a string literal) by passing it as argument to the constructor of string. But it is not a string, it's just some misleading terminology.
A similar case is calling things vectors when they are arrays.
If you use the type std::string in your code then you should include the <string> header. There are also a few other types and functions in that header, but std::string is the most commonly used one.
However, you do not need to include this header just to use string literals, which are built into the core language.
In your first case, library "string" is not needed. The object "cout" is supported by library "iostream", thus you have:
#include <iostream>
For the second case, you do explicitly use "string", thus library "string" is required:
#include <string>