Trying to run this program, but whenever it runs is quickly opens up and closes right away without allowing me to interact with it. What am I doing wrong to make it open and close?
Below is my program, and I'm sure I'm doing something wrong.
Thanks for your help!
Driver.cpp
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
char Anagram();
}
Anagram.cpp
#include<string>
#include<algorithm>
#include<iostream>
#include "Anagram.h"
using namespace std;
char Anagram()
{
string FirstAnagram, SecondAnagram;
char keep_going;
do
{
cout << "Enter word one: ";
cin >> FirstAnagram;
cout << "Enter word two: ";
cin >> SecondAnagram;
sort(FirstAnagram.begin(), FirstAnagram.end());
sort(SecondAnagram.begin(), SecondAnagram.end());
if (FirstAnagram == SecondAnagram)
{
cout << "They are anagrams of each other.";
}
else
{
cout << "They are not anagrams of each other.";
}
cout << "\n\nTry another?";
cin >> keep_going;
}
while (keep_going == 'y');
return 0;
}
Anagram.h
char Anagram();
Try this:
#include<string>
#include<algorithm>
#include<iostream>
#include "Anagram.h" // add this
using namespace std;
int main()
{
char ch = Anagram(); // call the function like this
}
Try adding this to the end of your main function:
std::cout << "Press Enter to close application.\n";
std::cin.ignore(10000, '\n');
Related
This question already has answers here:
How do I pass a cin'd c style string to a function?
(2 answers)
Closed last year.
Here is my code I am expecting the output but I am not getting .It stop after taking the input
I am expecting the output if i give name Harsh
Your name is Harsh
#include <iostream>
#include <cstring>
using namespace std;
int main() {
cout << "Enter your name" << endl;
char *s;
cin >> s;
cout << "Your name is " << s;
return 0;
}
I have also tried with cin.getline(s,100);but still it is not working.
So I request to you to solve the problem and give me solution.
Your code has undefined behavior because you are not allocating any memory for s to point at. s is an uninitialized pointer.
Try this instead:
#include <iostream>
using namespace std;
int main(){
cout << "Enter your name" << endl;
char s[100];
cin >> s; // or: cin.getline(s,100);
cout << "Your name is " << s;
return 0;
}
Alternatively, you should use std::string instead, eg:
#include <iostream>
#include <string>
using namespace std;
int main(){
cout << "Enter your name" << endl;
string s;
cin >> s; // or: getline(cin,s);
cout << "Your name is " << s;
return 0;
}
s in your code is unallocated.
Since it is C++ we're talking about, you probably don't want to use pointers and memory allocation, and use std::string instead.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
cout << "Enter your name" << endl;
string s; // Instead of dealing with char* allocation and memory issues.
cin >> s;
cout << "Your name is " << s;
return 0;
}
you have done it correctly but the problem with output is because of the memory allocation.
You have to allocate memory and try to avoid the concept of a pointer in that. Instead
Use string s;
or
char s[50];
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
getline(cin, s);
cout << s << endl;
return 0;
}
I think it will help someone with my question and answer.
In the code you showed, I think pressing only the Enter key will return an empty string.
I got a task from my teacher. I try some code but it confuses me a lot. So here's my code :
#include <iostream>
using namespace std;
char inputChecker [1000];
string source = "10110111000111001101110";
string detected;
int main(){
cout <<"Input:";
cin >> inputChecker;
for (int i=0;i<source.size();i++){
if (source[i]==inputChecker[0]){
cout <<"Data " <<inputChecker <<"is exist" <<endl;
}
else if (source[i]==inputChecker[i]){
cout <<"Data " <<inputChecker <<" isn't exist'" <<endl;
}
}
}
So ,my expectation output is ,when i input 10,it will result "Data 10 is exist". Without looping. I think it needed 2 kind of looping but i dont know where to loop.
My expectation output :
Input : 10
Data 10 is exist
Input : 25
Data 25 isn't exist
Thanks in advance :))
No need for loop
#include <iostream>
using namespace std;
int main() {
string source = "10110111000111001101110";
string input;
cin >> input;
if (source.find(input) != string::npos)
cout << input << " exists\n";
else
cout << input <<" doesn't exist\n";
}
Have a look at other useful std::string methods like find_first_of, find_last_of, etc.
I've recently started teaching myself C++, and after having written a lot of user input code, it's made me wonder if there's a simpler way of handling it.
For example, the normal way of doing it would be like this:
#include <iostream>
using namespace std;
int inp;
int guess = 13;
void main(){
cout << "Guess a number: ";
cin >> inp;
if (inp == guess)
cout << endl << "Nice.";
}
But what I want to do is:
#include <iostream>
using namespace std;
int guess = 13;
void main(){
cout << "Guess a number: ";
if (cin == guess)
cout << endl << "Even nicer.";
}
Is there a way to do this? Or this that just improper C++ standard?
In short: No, it's not possible to do as you want it.
You need to understand, that >> is actually a function call of
template<typename T>
std::istream& operator>>(std::istream& is, T& result);
and == is a function call to
template<typename T>
bool operator==(const std::istream&,const T& x);
Where the latter is used to check the stream state, and doesn't extract any user input.
To compare the input the result needs to be extracted from the std::istream in 1st place.
Well you can do it in one line but you don't really need to. But here are some examples anyway
//This will work for a char
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
char test = 'a';
if (getch()== test)
cout<<"\n Works";
return 0;
}
And if you really want
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int x =1;
int y;
for( cin >> y ; x == y ; )
{
cout<<"\n Works";
break;
}
return 0;
}
Or as NathanOliver said you could simply do this
if( cin >> inp && inp == guess )
But really you want to keep it simple as this will confuse others as well as yourself after some time. You want to leave your code as easy as possible
I cant find the error in this piece of code could anyone please insight me? I ran the debugging but the errors are un-understandable..
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
string name;
cout << "Input your name please?" << endl;
cin >> name;
if
{
(name == "Bart Simpson")
cout << "You have been very naughty" << endl;
}
return 0;
}
Problems:
You have some missing #includes, which probably caused your initial compiler errors.
You have a simple syntax error with your if statement.
Using the stream extraction operator will never yield a string with whitespace inside of it.
The following should work as you expect:
#include "stdafx.h"
#include <iostream>
#include <ostream>
#include <string>
using namespace std;
int main()
{
cout << "Input your name please?" << endl;
string name;
getline(cin, name);
if (name == "Bart Simpson")
{
cout << "You have been very naughty" << endl;
}
return 0;
}
(You need to include string for std::string and std::getline, and ostream for std::endl.)
I assume the bracket in the wrong place is just a problem when pasting the code
if(name == "Bart Simpson")
name will never equal "Bart Simpson", since extracting a string stops when it encounters whitespace; so it would only be "Bart". Perhaps you want to use getline() instead?
Should be
if (name == "Bart Simpson")
{
cout << "You have been very naughty" << endl;
}
And you need to include <string>