| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Okay, so I think I've come a long way. I'm learning C by thinking of a program, and then using this tutorial book I have to answer my questions while writing it. It's not interactive... go figure, right? okay.. This is what I want it to do. something like "you have 5 guesses to guess my name" This is what I have so far. My name is sterling, by the way. Does anyone know why this won't work??
Code:
#include <stdio.h>
main()
{
char guess;
char name[] = "sterling";
int count = 0;
char *pointer;
printf("Hello.\n");
printf("You have 5 tries to guess my name.\n");
do
{
printf("What is your Guess?\n");
scanf("%c",guess);
guess=*pointer;
if(name==pointer)
{
printf("Well done!\n");
break;
}
if(name!=pointer)
{
printf("Wrong!\n");
}
++count;
}
while(count<4);
}
|
|
#2
|
||||
|
||||
|
Hi ya, Its me again! =Þ I think I found a solution to your problem PreemptFallacy.
Code:
#include <cstdlib>
#include <iostream>
#include <stdio.h>
using namespace std;
main()
{
char guess[25];//changed guess to an array for functionality reasons
char name[] = "sterling";
int count = 0;
char *pointer;
printf("Hello.\n");
printf("You have 5 tries to guess my name.\n");
do
{
printf("What is your Guess?\n");
cin>>guess;//I changed scanf to a cin>> statement in order for this
//to function properly, well at least for me.
//in order to use the cin>> you need to add the
//<iostream> library or it wont work =)
if(!strcmp(name,guess))//strcmp compares a the two strings to see if there identical
{ //returns if correct but that you know =Þ
printf("Well done!\n");
break;
}
if(strcmp(name,guess))
{
printf("Wrong!\n");
}
++count;
}
while(count<4);
system ("PAUSE");//Pauses the system in order to view results
return 0;
}
Compile that and see how it works for you, but now make a ending for the person who fails all four attempts instead of just ending the game =) that would be cool. Thanks for your help too on my other question. That was a while ago but still thanks. I found out the problem was that I mest with the output of the compiler and I'm just not smart enough to put it back to were it was and the default settings didn't work for me either. If you want a C++ beginner book try (C++ primer plus by Stephen prata) it was written in 2005 so its information is up to date, its 1200 pages long so you can imagine that the descriptions of examples are much more thorough than most books I think you will find it helpful as I did. Its 50 bucks at BAM!! but its worth it. In my opinion C++ gives you a good base no matter if your working with C or C++. Well hope Ive helped me just a beginner =) |
|
#3
|
||||
|
||||
|
Change
Code:
if(!strcmp(name,guess))//strcmp compares a the two strings to see if there identical
{ //returns if correct but that you know =Þ
printf("Well done!\n");
break;
}
to Code:
if(!strncasecmp(guess,"Sterling",1))//strncasecmp compares the difference between lower and upper case letters
{
printf("Well done!\n");
break;
}
That way the user can enter either Sterling or sterling =) play around with those functions and see what you get, its been fun to help you with this. I dont use this very often in any of my practice programs so its nice to get some, because like they say if you dont use it you lose it!!! |
|
#4
|
|||
|
|||
|
Could you explain to me exactly how to declare an array, because I tried several times and it didn't work for me.
What is a cin>> statement, and also thanks for suggesting printed material. I'm going to go by that!! Thanks so much for all of your help! |
|
#5
|
||||
|
||||
|
No Problem I might Be Able To Help With That Too
Quote:
You declare an array just as you would any other variables but the only difference is the [], that just stands for the number of elements that variable can hold. Example: Code:
int this[5]; char that[25]; long float array[5]; Now you can also give each one values much like you did with your char[] ="sterling", but its a little different syntax to do that for numbers. Example: Code:
int this[5] = {1,2,3,4,5};//notice the curly brackets that
//seperate them along with the comma's.
long float array[5] = {0.25,.55,3.0,77}//If the last number
//is not defined the computer makes it a NULL or zero.
you can use them in much the same way for example if you wanted the first element of (int this) you would put (int this[0]) dont ask me y because my book didnt really explain y =Þ then it goes up. so basically it looks something like this. Code:
int this[0] = 1; int this[1] = 2; int this[2] = 3; int this[3] = 4; and you get the point so then you could use them just as if they were there own seperate variables that have the same name with different values. but lets say like in your program you need to use the whole array then you use it just like any other, just implement it were it needs to be and your good. Now, Quote:
a cin>> statement collects user keyboard input basically, its brother is the cout<< statement which is the replacement for prntf to some extent. the cin & cout statements need to have the header <iostream> included if you want to use them like that, but those statements to me are very handy. some ways to use them. Code:
include <iostream>
using namespace std;
int main(void)
{
char name[25];
int age[3];
char DOB[6];
cout<<"What is your name friend\n";
cin>>name;
cout<<"Well, how old are you\n";
cin>>age;
cout<<"What is your date of birth\n";
cin>>DOB;
system ("PAUSE");
return 0;
}
OUTPUT
What is your name friend
geo//user input from the cin
Well, how old are you
25//user input from the cin
What is your date of birth
5/13/1999//same here
Well hope those explanations helped Im not very good at describing things as I am in doing them =).. And NP man any time I can help, which aint much, I dont mind. |
|
#6
|
|||
|
|||
|
Your so helpful, do you have an e-mail address that I could send something to? How long have you been working with C?
|
|
#7
|
|||
|
|||
|
Quote:
So just to make sure I understand Line 1 declares an int named this with 5 characters Line 2 declares a char array named that with 25 characters and Line 3 declares an int array (formatted with floats) I'm I right on line 3, cause I'm really just guessing... lol Also on the cin & cout statements are they just a more advanced way to say printf and scanf, or are there specific senarios in which you would use each. As always thanks for all of your help! I think I understand what I have learned! Thanks so much. The next thing I'm going to try to do is combine the two of them by doing something like "You have five tries to guess a name and a number" and telling them which is right. (If they get the name tell them if they get the number tell them) I'm not quite sure how to go about it yet, but that's half of the fun.. lol Thanks again! Wish me luck! ![]() |
|
#8
|
||||
|
||||
|
OK PreemptFallacy, it sounds like you got a pretty good grasp on arrays but as far as the cin and cout statements they are used in C++ That's what language I started learning first so that's what I use. I should have started like you and started with C because that is typically the base of C++. But I didn't so I mainly use things that are primarily used in the C++ language so don't let me confuse what you are learning man. cin and cout statements in my opinion, are just C++'s way to make inputting and outputting information easier.
But Fo real get that book and skip to about the 25th page and read from there on, I know its an intimidating large book if you are like me and don't like reading but it will clear up a lot of confusion I promise you. Just for example I have read two books on the C++ language and I am still a little confused on certain areas but after I started reading that book, well, he just puts his descriptions in a easy to understand way. LOL you would think I'm the author the way I'm pushing this book on ya. And No problem man like I said, any time I got the time and I can help just let me know. |
|
#9
|
|||
|
|||
|
Quote:
I tried to compile that program you wrote to tell show me an example of cin>>statements and this is one of the errors I got: no match for 'operator>>' in 'std::cin >> age' on line 4 what is your make on that? |
|
#10
|
|||
|
|||
|
Quote:
Hey sign on to AOL, please |
|
#11
|
||||
|
||||
|
Quote:
Well I will give you an email address when we get together on AIM, and I have only been learning this for about five months or so. Mostly been concentrating on C++ though. Quote:
Well, my bad =) just take away the [3] behind age. It wasnt necessary to make age an array, I was just trying to show you how arrays and cin and cout statements function but I got a little carried away. so compile it with age looking like this // int age; // I am on aim now, alls you gotta do is hit the little yellow guy now =).. I will stay on for the rest of the day today. Sorry I didn't stay up like usual last night, I'm usually on at 1:00am and later but it caught up with me last night. You must be in a different time zone all together huh. Don't worry its Friday now and Ill probably be up late tonight =) |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Totally Lost... again! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|