Posted: Tue Jun 15, 2004 9:38 pm Post subject: Stupid Nub Question
Umm is it possible to write a statement in C++ similar to
a get statement in Turing where you can actually get a sentence or a word from the user ? eg..
code:
var a : string := ""
put "Enter your name"
get a
like cause i can't get more than 1 char from the "user"
--Thanks
Sponsor Sponsor
Catalyst
Posted: Tue Jun 15, 2004 9:41 pm Post subject: (No subject)
Posted: Tue Jun 15, 2004 10:00 pm Post subject: Re: Stupid Nub Question
eNc wrote:
Umm is it possible to write a statement in C++ similar to
a get statement in Turing where you can actually get a sentence or a word from the user ? eg..
code:
var a : string := ""
put "Enter your name"
get a
like cause i can't get more than 1 char from the "user"
--Thanks
Start out with the basic framework of a C++ program:
code:
int main()
{
}
Then you need the string library and the iostream library so you can deal with strings and input and output.
code:
#include <string>
#include <iostream>
int main()
{
}
Now, you'll want an equivalent for:
code:
var a : string := ""
code:
#include <string>
#include <iostream>
int main()
{
std::string a = "";
}
And then an equivalent for:
code:
put "Enter your name"
code:
#include <string>
#include <iostream>
int main()
{
std::string a = "";
std::cout << "Enter your name" << std::endl;
}
And an equivalent for:
code:
get a
code:
#include <string>
#include <iostream>
int main()
{
std::string a = "";
std::cout << "Enter your name" << std::endl;
std::cin >> a;
}
And just for fun, in Ruby
code:
puts "Enter your name"
a = gets.strip
And Python...
code:
import sys
print "Enter your name"
a = sys.stdin.readline().strip()
Perl...
code:
print "Enter your name\n";
chomp(my $a = <STDIN>);
O'Caml
code:
let _ =
print_endline "Enter your name";
let a = read_line () in
print_endline a;;
wtd
Posted: Tue Jun 15, 2004 10:04 pm Post subject: (No subject)
Don't use char arrays when you should use std::string.
code:
#include <string>
#include <iostream>
int main()
{
std::string line = "";
std::getline(std::cin, line);
}
Catalyst
Posted: Tue Jun 15, 2004 10:20 pm Post subject: (No subject)
havent used getline much (or at all), wasnt aware it took std::strings
eNc
Posted: Tue Jun 15, 2004 11:10 pm Post subject: (No subject)
wtd holy you kno so many languages!
wtd
Posted: Tue Jun 15, 2004 11:49 pm Post subject: (No subject)
Well, there are a few I didn't list there, and my knowledge of O'Caml is just starting to come together. Yet it's an incredibly nifty language. For instance:
code:
let plus1 = (+) 1;;
plus1 3;;
(* Same as... *)
1 + 3;;
Or a more practical example. The Printf module has a fprintf function similar to C's. But what if, for writing error messages out I don't want to always write:
code:
open Printf
Printf.fprintf stderr "%s\n" error_msg;;
I can instead write:
code:
open Printf
let print_err = Printf.fprintf stderr "%s\n";;
print_err "Oops...";;
eNc
Posted: Wed Jun 16, 2004 6:39 am Post subject: (No subject)