Create a guessing game till I want to stop - dev-c++

I'm new to programming, I'm trying to turn modify this program so that i can enter guesses until I want to stop. any help would be great!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main()
{
char name[10][20]= {"DUMBO","MICKEY MOUSE","GOOFY","DONALD DUCK"};
char charName[20];
int count;
char found;
printf("Enter the name of a Disney cartoon character ");
gets(charName);
strupr (charName);
found = 'n';
for (count=0;count<4;count++)
{ if(!strcmp(charName, name[count]))
{found = 'y';}
}
if(found == 'y')
puts("Match");
else
puts("No Match");
system("pause");
return;
}

A good way to keep it going is to use a while loop. I'm not that familiar with C++, but the general idea would be:
declare list of names
nameMatch = 0
while nameMatch == 0:
Ask the user to enter a name
if input == one of the names
puts("Match")
nameMatch = 1
else
puts("No Match")
That way it'll keep asking until the user picks the right name, or any other condition you want to set.

Related

Need help writing if else statement requiring specific keywords in dev c++

For my computer programming class they are requiring me to have someone input a date and for me to respond if it’s a real date or not Something like "September 10" would be a valid date; "triangle 44" would not and be met with the text “not valid”.
I’m trying to figure out why I can’t add multiple keywords to a if else statement so that if they put in one of the 12 months it would accept that, or else it would say it’s a not valid date. We use dev c++ if thats important.
For some reason whenever I add the months into the if (month=="sept") it will never go through the else statement even though I put in the wrong answer. I've even use || operators. I don't know what to do.
#include <iostream>
#include <iomanip>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
string month, VD;
cout<<"Please state name of month you wish to leave: ";
cin>>month;
if(month=="sept"){
cout<<"true";
}
else{
cout<<"false";
}
//"September","October","November","December","January","February","March","April","May","June","July","August"--months i need to add//
return 0;
}

How to detect user input of CTRL-X in C++

I need to end my function once the user inputs CTRL-X or '/'. I don't know how to detect/check for a user input of CTRL-X.
The task given to me states:
'When you edit the file, you will enter data line by line and when done, you will enter ‘/’ or CTRL + X to exit.'
I have written this code so far. I'm a beginner so pardon my code.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string data;
int line_num=1;
ofstream editFile;
editFile.open("edit_test.txt");
while(data!="/"){ //some kind of condition ORed with the condition already present in while loop?
cout<<line_num<<"> "; //I want to display line numbers every time the user presses enter
getline(cin, data);
if(data!="/"){
editFile<<data<<endl;
line_num++;
}
}
editFile.close();
return 0;
}
CTRL+X is the same as character code 24 (since X is the 24th letter of the alphabet). Barring any system interference*, all you need to do is check to see if character code 24 is in the input.
while (getline( std::cin, s ))
{
// Find ^C in s
auto n = s.find( '\x18' );
if (n != s.npos) s = s.substr( 0, n );
// process s normally here
// If ^C was found in s, we're done reading input from user
if (n != s.npos) break;
}
CTRL+X doesn’t tend to have any special system actions attached to it, so you shouldn’t have any problem getting it as input.

How to read string in text file then loop to another string?

I have been tackling this problem for almost two sleepless(?) weeks now. I'm creating a program that will read from a text file and prints getline() with a period '.' delimiter. I want to read each sentence AND also check for the strings "[INT]" and the like.
This program is a rudimentary text-based visual novel, and thus needs choices, stats and save algorithms.
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <ctype.h>
using namespace std;
#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_LEFT 75
#define KEY_RIGHT 77
#define ENTER 13
#define ESC 27
#define BACKSPACE 8
int main()
{
if(!loadfile)
{
cout<<"\n\n[Load a Saved File First.]\n\n\n\n";
system("pause");
//loadgame(); unimportant for now
}
menu = true;
system("cls");
ifstream novel("Novels\\main.vnt");
fstream backlog;
backlog.open("backlog.txt");
pagex=0;
/*while(pagex!=page)
{
//loop until pagex = page
}*/
string title = "title ", line;
getline(novel, line);
system(("title "+line).c_str());
while(menu)
{
c=0;
switch((c=getch()))
{
case KEY_DOWN:
getline(novel, line, '.');
line += ".";
backlog<<line;
page++;
cout<<line;
break;
case KEY_UP:
while(getline(backlog, logline))
{
cout<<logline;
system("pause");
}
break;
//case 's':
//case 'S':
// savegame(1, 's'); unimportant for now
case ESC:
menu = false;
//goto character; unimportant for now
break;
case ENTER:
/*In this part, the code should loop (using page as a counter)
until it reaches one of the special formats (like "[INT]",
"[PAT]", "[CHOICE]" and so on*/
break;
}
//character:
//character(); unimportant for now
system("pause");
return 0;
}
As for the format of the story:
Hello there.
[CHOICE]:
[INT]: I'm so smart.
[PAT]: I'm so patient.
[CHR]: I'm so charming.
[SAN]: I'm so sane.
[INT](
Yeah, right. I can't even finish this sente
)
[PAT}(
Yeah, right. You do know you should pass this tomorrow, right?
)
[CHR](
Yeah, right. Why does my heart beat fast?
)
[SAN](
Hurr, durr.
)
I've tried reading a line and saving it instead rather than using a number, but due to the sentence-based printing, I can't just up and save a line and expect the user to understand where he left off when he reads
"ere are you?", said the maid. Apparently, standardiza"
Please help.

Strings; substrings. Not understanding a block of codes

This is a program that creates a username for a user who enters their first name and last name.
Username = user's first initial + up to 7 letters of user's last name.
Ex: John Smith becomes 'jsmith'
John Smoother becomes 'jsmoothe'
#include <iostream>
#include <string>
using namespace std;
int main()
{
string fullname, fname, lname, uname, u2;
int l, len;
cout<<"FULLNAME: ";
getline(cin, fullname);
l=fullname.length();
cout<<"Fullname length: "<<l<<endl;
/*
len=0;
while (len!=(l-1))
{
if (fullname[len]!=' ')
len++;
else
break;
}
fname=fullname.substr(0,len);
lname=fullname.substr(len+1, (l-len));
*/
cout<<"FName: "<<fname<<endl;
cout<<"LName: "<<lname<<endl;
if(isupper(fname[0]))
{
fname[0]=fname[0] + 32;
}
if(isupper(lname[0]))
{
lname[0]=lname[0] + 32;
}
cout<<"FName: "<<fname<<endl;
cout<<"LName: "<<lname<<endl;
uname=fname.substr(0,1);
u2=lname.substr(0,7);
uname+=u2;
cout<<"USERNAME: "<<uname;
return 0;
}
I am having difficulties understand the block of codes i put between comments (/.../).
It is a silly code so do not try to understand it. It would have been much better if you had wrote the code by yourself. In this case, you would not ask such a question.
The author of the code tries to count first non-space characters and then to extract them using method substr and to place them in fname. Remaining characters are placed in lname.
Take into account that the code is invalid. For example the entered string can start with blanks. Also between the two names there can be more than one blank.
Also, it would have been much better if it was written for example
fname[0] = tolower( fname[0] );
instead of
fname[0]=fname[0] + 32;
And I think you need to convert all the characters of names to lower case.
It is simple ,if your string is "John Hoffman",len will equal to 4

C++ - Quitting a program

In the C++ Without Fear: A Beginner's Guide That Makes You Feel Smart book in chapter(8), part of a code trying to display a text file is the following:
while(1)
{
for(int i=1; i <= 24 && !file_in.eof(); i++)
{
file_in.getline(input_line,80);
std::cout<<input_line<<std::endl;
}
if(file_in.eof())
{
break;
}
std::cout<<"More? (Press 'Q' and ENTER to quit.)";
std::cin.getline(input_line,80);
c=input_line[0]; // <<<<<<
if(c=='Q'||c=='q')
{
break;
}
}
The part I'm not getting here is:
c=input_line[0];
I think it is put to read 'Q' or 'q'. But, why using this form (Array)? And, isn't there a way to read 'Q' or 'q' directly?
I tried std::cin>>c; but seemed to be incorrect.
Any ideas?
Thanks.
Because input_line is string ( array from chars), so input_line[0] gets the first letter - this is in case, that the user write "quit" or "Quit", not just "Q"
std::cin >> c; would be correct, if you enter just one char and press Enter
I tried std::cin>>c; but seemed to be incorrect.
That's correct, if c is a char.
You're right; reading an entire line just to extract a single character is bizarre. I recommend a book from this list.
You are getting the first character from the "array" into which the input line has been written.
NON-STANDARD solution, but works on windows platforms.
you can use getch() function defined in conio.h
example:
#include <conio.h>
...
char c = getch();
bye