Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm learning to use C++ and I decided to create a password program where the user is asked for the password and it compares the user input to the password and returns a wrong or a right. For some reason, this programs always returns a wrong and I'm not sure why. It must be something to do with comparing the strings but I'm not sure.
#include <iostream>
#include <string>
using namespace std;
int main(){
string pass = "password";
string input;
cout << "What is your password: ";
cin >> input;
if (input==pass){
cout << "Correct" << endl;
}else{
cout << "Wrong" << endl;
}
return 0;
}
I would love some help from programmers who are in any way more well versed in C++ as I've just transferred over to C++ from Python and the transitions a bit rocky.
1.you could use compare function, to see:http://www.cplusplus.com/reference/string/string/compare/
2.you should debug at line if (input==pass){
to print pass and input and check if they are the same.
I found I needed to:
#include <string>
to get the definition of the insertion operator (for cin >> input;) and std::string::operator==() (for if (input==pass)). Once I did that, it worked fine in Visual C++.
What compiler are you using?
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am new to coding and I was wondering how can you print out two different decimal places within the same line. For instance, the first value should have one decimal place and the second value should have three decimal places. I know how to use setprecision to have different decimal places for different lines, but not how to have it on the same line.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double precision_value = 12.409583385;
cout << fixed << setprecision(2) << precision_value << " " << setprecision(4) << precision_value;
return 0;
}
A few things to note:
In the same print statement, the fixed only needs to be set once and works for the remainder of the stream. The setprecision() can be updated to any value you need after that.
Also, prefer to remove using namespace std; and utilize the namespace std:: wherever required. But, for demonstration purposes, this suffices for your example needs.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Very simple question but unfortunately I am at a loss. I am trying to build a program where a user is asked to enter nicknames for a pet. Each time they enter a nickname I would like to store that nickname into an array. After they enter a nickname I would also like to list all of the nicknames that they previously entered as well. I would provide some source code but I am not sure where to begin. Any tips or feedback would be greatly appreciated!
Using C++ 11, a sample code with comments is provided.
// header file for standard i/o stream
#include<iostream>
// header file for string
#include<cstring>
// header file for vector container usage
#include<vector>
// use standard namespace, std
using namespace std;
// main start from here
int main() {
// use vector which is a c++ stl container
vector<string> names;
// a temp var
string name;
// input until EOF
while (getline(cin, name)) {
// push back to the vector container
names.push_back(name);
// output what are inside the vector
// Here I use C++ 11 auto feature
for (auto pet_name : names) {
cout << pet_name << ", ";
}
cout << endl;
}
return 0;
}
I would use std::vector.
Internally std::vector use a dynamically allocated array to store their elements.
So you can simply use std::vector::push_back(petNickName) to store nickname of pet represented as string and not worrying about size of array and other things... .
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 7 years ago.
Improve this question
I was trying to solve a really simple problem on UVa online judge. The problem code is: 10071. You can find the problem here: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94
My code looks like this:
#include<iostream>
using namespace std;
int main(){
int v,t,a,d;
cin >> v >> t;
t = t*2;
d = (v)*t;
cout << d << endl;
}
But it says wrong answer. What went wrong and how to solve it?
You have not read the complete question.
Correct solution is as follows:
#include <stdio.h>
int main()
{
int a,b,c;
while(scanf("%d%d",&a,&b)==2)
{
printf("%d\n",(a*b)*2);
}
return 0;
}
As you may notice above, there can be multiple test cases. You have to account for it. So I have a while loop for it.
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
I'm using strcpy to copy a word from sentence into array of string. But this code is not working as expected. Please point out error if any..
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
int i=0,j,k;
char a[100],*str[100];
char *b;
cout<<"enter a sentence:\n";
cin.getline(a,100);
str[0] = strtok(a," ");
while(str[i]!=NULL)
{
i=i+1;
k=strlen(strtok(NULL," "));
str[i]=new char[k+1];
strcpy(str[i],strtok(NULL," "));
}
for(i=0;;i++)
{
if(str[i]!=NULL)
cout<<"\n"<<str[i];
else
break;
}
return 0;
}
Two successive calls strtok(NULL," ") will not return twice the same pointer to a word in your string, as you seem to expect, but rather pointers to successive words. On the other hand you do seem to expect to get a pointer to a new token when you go around your while loop. That is not how things work.
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
The following Code is not working. I get an error at the command cin >> h. What am I doing wrong?
#include <iostream>
using namespace std;
int main()
{
string h = " ";
cout << "hi" << endl;
cin >> h;
cout << h << endl;
system("pause");
return 0;
}
Random guessing:
You forgot to #include <string>
You forgot to include <string> and C++ punished you for that.
Ah, but every man and his dog should know that by not including <string>, you were using the default >> operator, that has well know issues with strings.
C++'s "leave the progammers free to shoot themselves in the foot" philosophy at its best.
C++ lore tells the unfortunate wandereds should use getline instead of cin >>, but there have been heated debates among scholars on this fine doctrine point.