All strings are unidentified - c++

#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.

Related

I don't understand why my stoi says it is not declared when it supposedly is [duplicate]

my question is pretty simple but I can't seem to find it out.
I want to know what libary to include when using stoi. I was using atoi and it works fine with
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
but I get "stoi not declared" when I run with stoi. Thanks
You need to #include <string> and use a compiler that understands C++11. Minimal example:
#include <string>
#include <cassert>
int main()
{
std::string example = "1234";
int i = std::stoi(example);
assert(i == 1234);
return 0;
}
Compile, for example, with g++ -std=c++11.

Where to get _fileno and _O_U16TEXT?

I'm trying to print the text "Ääkkösiä ruutuun." to console with c++. I have windows 7 and am using Code::Blocks editor. Searching on the subject I found that maybe these sort of lines would help
_setmode(_fileno(stdout), _O_U16TEXT);
wstring s{L"Ääkkösiä ruutuun."};
wcout<<s<<endl;
But when I try to compile it, I get the error: _fileno was not declared in this scope.
I have all these includes:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stdexcept>
#include <cmath>
#include <sstream>
#include <fstream>
#include <codecvt>
#include <locale>
#include <fcntl.h>
#include <io.h>
#include <stdio.h>
#include <cstdio>
#include <ostream>
what am I missing?
Also, one other thing I tried was locale, but then locale::empty wasn't found! Why doesn't my c++ have anything in it?
EDIT
Here is a picture of what my program is doing now.
It prints out just the first letter (Ä). What happens to the rest?
Ok, it seems that setmode sets it so that only one letter gets printed. (Even trying to print normal texts with multiple commands, just results in a single letter.) Without it the scandinavian letters don't print correctly, thought. They look like this:
The answer you found is for Visual Studio, not Code::Blocks.
While the C standard specifies what should in in <stdio.h>, it only specifies a minimum. Implementors may add their own functions, and should do so using an _ (underscore prefix). This is why you should NOT use that prefix. You don't know what you'll break. Microsoft clearly signaled their non-standard extensions using the correct prefix.
The answer is tagged C++, but C++ inherits the contents of <stdio.h> from C.
The line
setlocale(LC_CTYPE, ".OCP");
works!
A complete example:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
wstring readFile(const char* filename) {
wifstream wif(filename);
locale myLoc("");
//locale utf8_locale(locale(), new gel::stdx::utf8cvt<true>);
wif.imbue(myLoc);
basic_stringstream<wchar_t> wss;
wss << wif.rdbuf();
return wss.str();
}
int main() {
setlocale(LC_CTYPE, ".OCP");
wstring contents = readFile("test.txt");
wcout<<L"Does anything get printed out at all???"<<endl;
//wcout <<contents<<endl;
wstring s{L"Ääkkösiä ruutuun."};
wcout<<s<<endl;
wcout<<L"Näkyykö äkköset?"<<endl;
return 0;
}
The text read from file (utf-8) still doesn't print correctly, though.
It should be
Hei!
Täällä on kaksi riviä.
ä's go awry there.
Output:

Invalid Template Argument for vector instantiation

When I run this code I get "invalid template arguments" error on the last line. Please advise. (I've omitted the rest of the code)
#include <iostream>
#include <fstream>
#include<array>
#include <vector>;
using namespace std;
int fileLineCount(string);
int fileExists(string[],int);
int main() {
ifstream archiveFile;
archiveFile.open("StudentRecords.txt");
int lineCount=fileLineCount("StudentRecords.txt");
string line;
vector<string> recordArray;
#include <vector>;
should be
#include <vector>
and of course you need to close the } brace at the end of main() (although probably this was a typo). You should also #include <string>, although some of your headers seem to include it implicitly (probably <iostream>).

Error when using conio.h to change text colour

Hey just found a library i want to use called coni.h to change text colour. But im getting an error in one of the functions: textcolour(RED); It says that it is undefined.
#include <iostream>
#include <string>
#include <conio.h>
#include "Storyline.h"
using namespace std;
int main()
{
Storyline story;
story.Story("Title.txt");
textcolor(MAGENTA+BLINK);
cout << "Hello";
getchar();
getchar();
}
I simply used system("color 0a"); from the "Windows.h". And i now have what i needed.

question on string in c++

does this work on string in c++?
string s="lomi";
cout<<s<<endl;
what is bad in this code?
#include <iostream>
#include <cstring>
using namespace std;
int main(){
string s=string("lomi");
for (int i=0;i<s.length();i++){
s[i]= s[i]+3;
}
std::cout<<s<<std::endl;
return 0;
}
?
Yes.
(after you have #included the corresponding headers, and using the std namespace, etc.)
Edit: What's wrong with your code is you should
#include <string>
instead of
#include <cstring>
cstring is C's string.h header, which defines functions like strlen, strcpy, etc. that manipulates a C string, i.e. char*.
string defines C++'s string class which you're using.
short answer: yes
long answer: string s = "lomi"works due to the string(const char*) constructor.
Works for me -- does it work for you?
Remember to do this first:
#include <ostream>
#include <string>
using namespace std;
yes, cout in C++ knows how to deal with strings
Yes it should compile and work, if you want to print "lomi\n", and you have included <string> and <ostream> and declared using namespace std;.