Jump to content

Uninitialized local variable?

FakezZ
Go to solution Solved by Ciccioo,

But if I use strings how will i be able to compare them?

with a simple ==

that's the power of strings, and of the standard namespace: you will be able to use all the "built-in functions and behaviours"

the namespace knows how to compare strings, so

if(string1 == string2)

works just the way you would expect it to

I am using visual studio express 2013. It works on my machine, although I did follow your advice and now the code is only c++ and not a mix :D

nope, hold on i'll make it clear:

 

 

notice the headers, the functions, the variable types

#include <stdio.h>#include <string.h>int main(){    char test[10];    scanf("%s", test);    if(strcmp(test, "ohoo") == 0){        // if equals    }else{        // if different    }    return 0;}
#include <iostream>#include <cstring>using namespace std;int main(){    string test;    cin >> test;    if(test == "ohoo"){        // if equals    }else{        // if different    }    return 0;}
Link to comment
Share on other sites

Link to post
Share on other sites

For reading unformatted input from the keyboard you should use getline() instead. Buffer overruns are bad juju. 

 

getline() will read until there are no more characters in the buffer or buffer length is reached. Newlines are discarded.

 

For char* fixed buffers

#define BUFFER_WIDTH 10char buffer [ BUFFER_WIDTH ] ;std::cin.getline ( buffer, BUFFER_WIDTH ) ;

std::string version

std::string buffer ;std::getline ( std::cin, buffer ) ;

and for the love of all that is holy, never use scanf. Use fgets() instead. It works like getline(). sscanf or strtol/strtod (preferably these) can extract numbers if you need em.

#define BUFFER_WIDTH 10char buffer [ BUFFER_WIDTH ] ;fgets ( buffer, BUFFER_WIDTH, stdin ) ;

main(i){for(;i<101;i++)printf("Fizz\n\0Fizzz\bBuzz\n\0%d\n"+(!(i%5)^!!(i%3)*3)*6,i);}

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×