string inserting error c++ - visual-c++

WordChosenDuplicate.insert(0,WordChosen.length," _ ");
cout <<WordChosenDuplicate<< endl;
I get the following error when I try to run this code
error C3867: 'std::basic_string<_Elem,_Traits,_Ax>::length': function call missing argument list; use '&std::basic_string<_Elem,_Traits,_Ax>::length' to create a pointer to member c:\documents and settings\main\my documents\uni\2nd year\tp2\hangman\hangman\hangman.cpp 119

It's a function: std::string::length()
Please read the Manuals available on the net and the answers posted to your previous questions.

You need to use WordChosen.length (). Add the parenthesis, this is a method call.

for (int f = 0; f <= WordChosen.length()-1;f++)
{
WordChosenDuplicate.insert(0,(WordChosen.length(),"_ "));
}
cout <<WordChosenDuplicate<< endl;
Thanks guys got the answer.

Related

How does visual studio read instructions from code?

Hi there StackOverflow community. I’ve faced a certain issue in understanding how visual studio processes the ‘instructions’ given by the code.
the code I’ve written was giving me a problem when i was running it.
It went something like:
whats your guess?
donkey
whats your guess?
donkey
your guess was: donkey
whats your guess?
dog
whats your guess?
dog
your guess was: dog.
It works correctly on alternative tries.
However, if i were to remove ' GetGuess();' on line 38. everything works perfectly fine.
However, it just bothers me that the Getguess on line 38 is nt require because the thought im having right now is that it has to process Getguess first before it processes giving back the guess.
Thank you
code:
#include <iostream>;
#include <string>;
using namespace std;
void PrintIntro();
void play_game();
string GetGuess();
// entry point for out application
int main()
{
PrintIntro();
play_game();
}
//intro game
void PrintIntro()
{
constexpr int WORD_LENGTH = 5;
cout << "welcome to bulls and cows\n";
cout << "can you guess the " << WORD_LENGTH << " letters word?\n";
return ;
}
void play_game()
{
// loop for number of turns asking for guesses
constexpr int number_of_turn = 5;
for (int count = 1; count <= number_of_turn; count++)
{
GetGuess();
string guess = GetGuess();
cout << "your guess was:" << guess << endl;
}
}
//gut guess from player
string GetGuess()
{
// ask for a guess
cout << "whats your guess?\n";
string guess = "";
getline(cin, guess);
return guess;
}
A piece of advice: When you run the code please put breakpoints, especially if you want to know how your code run.
"It works correctly on alternative tries. However, if i were to remove ' GetGuess();' on line 38. everything works perfectly fine. However, it just bothers me that the Getguess on line 38 is nt require because the thought im having right now is that it has to process Getguess first before it processes giving back the guess."
To answer your question, first your must know that, when you call "string guess = GetGuess();" the compiler already retrieve the return value and store it into "guess" (in this case, the function return guess), therefore you don't have to add GetGuess() anymore (this is redundant). Your thinking that the compiler needs to "process Getguess first" is incorrect since "string guess = GetGuess();" is already processing/retrieving the return value.
Also, I think it's a good practice to initialize and declare all functions before int main() but then, it's up to how you organize all your code functions.

Unable to use Iterator for a MAP to PRINT STRING. (able to print int)

I tried printing a string in the MAP using iterator but I am getting an error.
Program -
#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<map>
#include<string.h>
#include<unordered_map>
using namespace std;
void main()
{
std::map<int,std::string>mymap;
std::map<int,std::string>::iterator it;
mymap.insert(make_pair(10, "sid"));
mymap.insert(make_pair( 20, "sam"));
for (it = mymap.begin(); it != mymap.end(); it++)
{
//printf("%s \n", it->second);
std::cout <<*it->second << std::endl;
}
system("pause");
_getch;
}
Error list
1.Error C2679 binary '<<': no operator found which takes a right-hand
the operand of type 'std:: string' (or there is no acceptable conversion)
2.Error (active) no operator "<<" matches these operands
I was able to print the int properly. I am unable to print the STRING. Please suggest a solution.
That's not how you access the object pointed to by the iterator.
Just use cout<< it->second<<endl;.
All the errors are then automatically resolved.
P.S. Refer to my comment on your question.

can't pass operation '&' result to cout

I have two integers and trying to pass them to cout.
int a =1;
int b= 3;
cout<<a&b;
Compiler tells:
Error 2 error C2676: binary '&' : 'std::basic_ostream<_Elem,_Traits>' does not define this operator or a conversion to a type acceptable to the predefined operator
But a&b returns int that is understandable for '<<' operator.
Why this error rises?
Due to operators precedence, you need to use parenthesis:
cout << (a & b)
The << operator binds more tightly than &, so omitting the parenthesis makes the
compiler undertand it as (cout << a) & b, which explains the error report: The & operator can't be used with a stream (the returned object from cout << a) and an int.
This is a precedence problem, if I'm not mistaken. Try using cout << (a&b); and see if it doesn't work a bit better.
you can do it like this: :) or did i misunderstood? (a)
int a =1;
int b= 3;
cout<<a << "&" << b;

c++ c_str adding strange characters to the end of the string

let me first thank you for taking the time to read this.
I'm trying to read a file in c++. Currently I have a method that allows the user to select a file in explorer and returns this as an 'std::string'. I then have to open this file, but the method I have for this uses const char*. Therefore I need to convert from one to the other.
If there is an easy method in windows for reading a file using a string instead then let me know as it would solve my entire problem.
When I convert from string to const char*, using str.c_str(), I get a lot of weird characters at the end. I've researched other topics with the same problem, but the answers all seem very specific for those projects, or say just stick to sting/vector instead. Obviously I would happily do this, but I don't have a method that opens the file using a string/vector.
Any help is appreciated :) The code and output of where this occurs is pasted below.
*SOURCE
std::string f;
if (LOWORD(wParam) == 1) {
f = openFile();
char *fchar = new char[f.size()+1]; // +1 to account for \0 byte
//char *fchar = std::vector[1];
std::strncpy(fchar, f.c_str(), f.size());
file1 = fchar;
std::cout<<"string size: " << f.size() << std::endl;
std::cout<<"string: " << f << std::endl;
std::cout<<"fchar: " << fchar << std::endl;
std::cout<<"file1: " << file1 << std::endl;
}
OUTPUT
string size: 33
string: C:\Users\Joseph\Pictures\back_raw
fchar: C:\Users\Joseph\Pictures\back_raw═²²²²½½½½½½½½¯■
file1: C:\Users\Joseph\Pictures\back_raw═²²²²½½½½½½½½¯■**
std::strncpy() does not place a `\0' character at the end of the string. See http://www.cplusplus.com/reference/clibrary/cstring/strncpy/. You need to copy that as well:
std::strncpy(fchar, f.c_str(), f.size() + 1);
Where is file1 declared ?
What about openFile ? (would like to know its return type)
Could you try to manually put that '\0' ? IMHO, your char* is simply lacking the \0 at the end...
Make sure you put a \0 at the end, everywhere you need to.

Reading a value in associative array creates a new key

I have code such as this. I use
pvalueholder is class that is polymorphic , it can hold all sort of types, string..etc..
It also can have a type undefined.
typedef hash_map<pvalueholder,pvalueholder,pvaluehasher > hashtype;
hashtype h;
pvalueholder v;
v="c";
h[v]=5; // h has one element
pvalueholder v2=h[v]; // here h gets a new key/value how is that possible?
cout << (string) (h[v]) << endl; // here h gets another new key/value how is that possible?
int i =0;
for (hashtype::iterator h1=h.begin(); h1!=h.end();h1++)
{
cout << "no: " << i++ << endl;
} // this prints three lines, it should print one...
Two values are undefined here, the third one is 5 as expected.
size_t pvaluehasher::operator() (const pvalueholder& p) const
{
cout << "hashvalue:" << p.value->hashvalue() << endl;
return p.value->hashvalue();
}
returns
Here is what is printed:
hashvalue:84696444
hashvalue:84696444
hashvalue:84696444
returns:1
hashvalue:84696444
returns:1
hashvalue:84696444
returns:1
returns:1
hashvalue:84696444
Do you have any ideas what it may be?
Thank you.
Solution:
the function operator()(parameter1,parameter2) needs to be different in case of Microsoft STL.
For microsoft, it needs to return less than relationship between parameter1 and parameter2.
For gcc, it needs to return equality. I returned equality.
The comparison function for the keys was not correct...
The function returned true for equality while it has to return less than in case of Microsoft STL.
My guess would be that your hash function is incorrect - meaning it produces different hash values given the same key "c".
Show the declaration for pvalueholder and full code for pvaluehasher.
It's almost impossible to comment on hash_map, because it's never been standardized, and the existing implementations aren't entirely consistent. Worse, your code doesn't seem to be correct or compilable as it stands -- some places the value associated with the key seems to be an int, and other places a string.
Using std::tr1::unordered_map and fixing the rest of the code to compile and seem reasonable, like this:
#include <unordered_map>
#include <iostream>
#include <string>
using namespace std;
typedef std::tr1::unordered_map<std::string, int> hashtype;
std::ostream &operator<<(std::ostream &os, std::pair<std::string, int> const &d) {
return os << d.first << ": " << d.second;
}
int main() {
hashtype h;
std::string v = "c";
h[v]=5; // h has one element
int v2=h[v];
cout << h[v] << endl;
int i =0;
for (hashtype::iterator h1=h.begin(); h1!=h.end();h1++)
{
cout << *h1 << endl;
} // this prints three lines, it should print one...
return 0;
}
The output I get is:
5
c: 5
This seems quite reasonable -- we've inserted only one item, as expected.
Solution: the function operator()(parameter1,parameter2) needs to be different in case of Microsoft STL. For microsoft, it needs to return less than relationship between parameter1 and parameter2. For gcc, it needs to return equality. I returned equality. The comparison function for the keys was not correct... The function returned true for equality while it has to return less than in case of Microsoft STL.

Resources