Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm trying to set filename based on condition but it's not working. It keeps saying (null).
void main()
{
int cond = 1;
char * filename;
// C:\other\path\here\
filename = "C:\\other\\path\\here";
if (cond)
// C:\some\path\here\
filename = "C:\\some\\path\\here";
printf("%s", filename);
}
From the standard,
§2.1.2 [lex.phases]
Each instance of a new-line character and an immediately preceding
backslash character is deleted, splicing physical source lines to form
logical source lines.
So
// C:\other\path\here\
filename = "C:\\other\\path\\here";
becomes
// C:\other\path\here\filename = "C:\\other\\path\\here";
Likewise
// C:\some\path\here\
filename = "C:\\some\\path\\here";
becomes
// C:\some\path\here\filename = "C:\\some\\path\\here";
Hence filename never gets initialized.
The MS C++ compiler gives a warning.
(6) : warning C4010: single-line comment contains line-continuation character
(9) : warning C4010: single-line comment contains line-continuation character
After preprocessing, this will be the code, I think
void main()
{
int cond = 1;
char * filename;
if (cond)
printf("%s", filename);
}
Actually, in the above program filename can have any value, not necessarily null. So your program could print anything, crash, whatever. You are probably compiling debug & your compiler null initializes unitialized pointers in debug mode & hence you get null
Ob: main is always int main not void main
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
I'm working on this function for one of my classes and my pass count works just fine, however, my fail count ALWAYS prints out 12. I've been reading my code top to bottom and just can't seem to find the problem.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
string passAndFailCount(string grades){
int pass_count;
int fail_count;
istringstream myStream(grades);
string grade;
while(myStream>>grade){
int i_grade=stoi(grade);
if (i_grade>=55){
++pass_count;
}
else{
++fail_count;
}
}
cout<<"Pass: "<<pass_count<<endl<<"Fail: "<<fail_count<<endl;
}
int main()
{
string grades;
getline(cin, grades);
passAndFailCount(grades);
}
Your problem are uninitialized variables.
int pass_count = 0;
int fail_count = 0;
and you're set.
For an explanation. Non-global variables (which automatically get initialized to 'default' (0) as per the standard), automatic and dynamic variables (like the one you are using), assume the value of 'whatever was in memory at the time of allocating the variable'.
Memory is never empty, there's always 'something' written in there.
So the memory of your fail_count variable just 'happened to be' 12 during allocation, which is why you start with that value. This value can be anything within scope.
By explicitly assigning a variable, you 'initialize' the variable, putting it into a defined state (0) and your program should start working as expected.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
The function it has an issue with:
string encode (string message, string key) {
string code = "whatever";
string forst;
int num;
string::size_type begin = 0;
message = lower_and_strip(message);
for (char val : message) {
num = return_encoded_char(key, begin, val);
forst = to_string(num);
code.push_back(forst); //*******************************
}
return code;
}
The starred line is what it points to. The return_encoded_char function returns an integer.
The specific error is
proj05.cpp:68:23: error: no matching function for call to 'std::basic_string<char>::push_back(std::string&)' and points to the line I starred.
I initially just declared code without initializing it, but changing that didn't fix it. All the similar questions I could find had some other element to blame; I feel like this should be relatively straightforward, though obviously it's not since it isn't working.
I have #include <stream> and using std::to_string etc. I'm using -std=c++11 to compile it.
Help.
P.S. Using Geany on Linux.
Your code variable is a std::string. The std::string class doesn't have a push_back() method that takes another std::string as input. You should try with the += operator instead, which accepts a character or a string:
string encode (string message, string key) {
string code = "whatever";
string forst;
int num;
string::size_type begin = 0;
message = lower_and_strip(message);
for (char val : message) {
num = return_encoded_char(key, begin, val);
forst = to_string(num);
code += forst; //*******************************
}
return code;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
this is my c++ homework, please help me. (I already googled it and found nothing)
int main()
{
std::cout<<'123';
getchar();
return 0;
}
in output you'll get 3224115.
why?
Write instead
std::cout << "123";
^ ^
'123' is a character literal that has implementation defined value and type int.
It can be imagine for example the following way
#include <iostream>
int main()
{
int x = 0x313233;
std::cout << x << std::endl;
return 0;
}
where 0x31, 0x32, 0x33 are ASCII codes for characters '1', '2', '3' correspondingly.
For this program the output can be
3224115
Quoting the C standard (pg 61)
The value of an integer character constant containing more than one
character (e.g., 'ab'), or containing a character or escape sequence
that does not map to a single-byte execution character, is
implementation-defined.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
This code compiles and runs though gives a Microsoft compiler error that I cant fix
warning C4700: uninitialized local variable '' used.
This is in the starting line of the code, I think
void employee::loginemployee()
{
char uname[15];
char pass[15];
char p;
int i=0;
cout<<"\n\t\tEnter User Name :-";
cin>>uname;
puts("\n\t\tEnter Password :-");
while(p!=13)
{
p=_getch();
_putch('*');
pass[i]=p;
i++;
}
pass[i]='\0';
ifstream objdata;
objdata.open("HRStaff",ios::in|ios::out|ios::binary|ios::app);
if(!objdata)
{
cout<<"\n-----Cannot Open the File-----\n";
//return 1;
}
int nflag=0;
while(!objdata.eof())
{
objdata.read((char *)& info, sizeof(info));
if(strcmp(uname,info.uname)==0 )
{
system("cls");
cout<<"\n\n\n\t\t****************************************";
cout<<"\t\t\t\t\t\t\t Welcome TO EMS"<<info.uname<<endl;
cout<<"\t\t****************************************\n"<<endl;
info.putdata("SPS");
cout<<"\n\tPress any key to log out...";
nflag=1;
}
}
if(nflag==0)
{
cout<<"\n\nSorry !! Your Username & Password do not match.";
_getch();
logoutAll();
}
objdata.close();
}
The warning is quite clear. You declare a variable without initialising it:
char p;
then use its uninitialised value:
while(p!=13)
{
// ...
}
Either initialise it before use:
char p = 0; // or any value other than 13
or restructure the logic so its value isn't used until you've assigned to it:
do
{
// ...
} while (p != 13);
Then learn about buffer overflow and stop reading user input into fixed-sized buffers without checking the length. This is C++, not C, so you should usually use std::string to store string values.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am trying to find the occurrence of characters of one string(s1) in other string(s2).
This is part of my code.
for(;i<strlen(s1);i++)
{
int x=strchr(s2,s1[i]);
if(x>0)
count++;
}
But on compiling I get an error pointing to strchr() and says
error: invalid conversion from ‘char*’ to ‘int’ [-fpermissive]
Anyone explain what is problem in using strchr() function.
Assignment is wrong strchr doesn't returns int but returns address of first char in string found:
int x=strchr(s2,s1[i]);
should be:
char* x = strchr(s2, s1[i]);
// ^ returns char*
Read manual
char *strchr(const char *s, int c);
RETURN VALUE
The strchr() and strrchr() functions
return a pointer to the matched character or NULL if the character
is not found. The terminating null byte is considered part of the
string, so that if c is specified as '\0', these functions return a
pointer to the terminator.
And so:
if(x>0)
should be:
if(x != NULL)
or just if(x)