Ultimate Gear War
Join the alien war, prepare your gear and protect your base at all cost!
4.18 / 5.00 15,844 ViewsIndeed I am. I already know a bit of basic guff like ints and doubles, and even comparisons. Does anyone know any good but basic tutorials I could use?
Alright. So I've made a basic program:
#include <cstdlib>
#include <iostream>
using namespace std;
int main(void)
{
char string[50];
printf("HAHAHA! \n");
getchar();
scanf("%s", &string);
printf("\"%s\", He says!", &string);
system("PAUSE");
}
and it works for the most part, but it cuts out the first letter on the printf("\"%s\", He says!", &string); function. EG "ASS" comes out as "SS". What gives?
SHIT! I had my editor set to C++. It still does that in C mode.
I haven't gotten around to any C yet, but Stanford's Essential C (pdf) seems like a pretty decent start.
"If loving Python is crazy then I don't want to be sane." -Diki
I cannot recommend Learn C the Hard Way enough.
It's one of the the best books I've found on C, and it's free.
The C Programming Language is another excellent book, and it's co-authored by Dennis Ritchie, the designer of the C language.
At 9/20/12 12:01 PM, MikeyS9607 wrote: and it works for the most part, but it cuts out the first letter on the printf("\"%s\", He says!", &string); function. EG "ASS" comes out as "SS". What gives?
Well you have more than one problem here.
First you're mixing C and C++. <iostream> is a C++ header, and you're not returning an integer at the end of main(). Only C++ allows you to exclude that; you must always return an integer in C.
Secondly the string is being printed incorrectly because, I am pretty sure at least, you're passing a pointer to the string rather than the string itself (that's that the & operator does).
To print a string you simply just do this:
#include <stdio.h>
int main()
{
char* foo = "hello world";
printf("%s", foo);
return 0;
}
Or if you want to use an array instead of a char pointer:
#include <stdio.h>
int main()
{
char foo[50];
scanf("%s", foo);
printf("%s", foo);
return 0;
}
You can pretty much think of an array as a pointer. It behaves the same way.
Woo triple post (I'm fancy).
using namespace std;
This is also C++ code; there is no std namespace in C.
At 9/20/12 02:29 PM, Diki wrote: Woo triple post (I'm fancy).
using namespace std;This is also C++ code; there is no std namespace in C.
Yeah, i know. Thanks for the help.
At 9/20/12 02:29 PM, Diki wrote: This is also C++ code; there is no std namespace in C.
No namespaces in C either.
#include <stdio.h>
char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";
main() {printf(p,10,34,p,34,10);}
At 9/21/12 11:10 PM, Redshift wrote: No namespaces in C either.
Ah, that's right.
I'm not as well versed in C as I should be and that slipped my mind. :)
The book is " The C Programming Language " by "Brian W. Kernighan, Dennis M. Ritchie"......
Very good book......
Applications Programming in ANSI C, by Johnsonbaugh & Kalin.
This book. This book is your best friend.
At 9/20/12 02:10 PM, Diki wrote: Learn C the Hard Way
I'm starting to see a pattern, here. Same author as "Learn Python the Hard Way" and "Learn C++ the Hard Way"?
Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P
At 10/9/12 10:12 AM, egg82 wrote:At 9/20/12 02:10 PM, Diki wrote: Learn C the Hard WayI'm starting to see a pattern, here. Same author as "Learn Python the Hard Way" and "Learn C++ the Hard Way"?
At 10/9/12 10:12 AM, egg82 wrote: I'm starting to see a pattern, here. Same author as "Learn Python the Hard Way" and "Learn C++ the Hard Way"?
Yep. Same author; he gives all his books the same title.
At 10/9/12 03:48 PM, m0lecule wrote: Afraid so...
"Afraid so"? You say that like it's a bad thing.