I guess by 'string' you mean a character-array; so the answer is: an array in itself is a pointer to its first element: arr==&arr[0].
(Note: it is a pointer-constantant, not a pointer-variable, so you cannot change it's value: 'arr= something' is wrong.)
No.
The safest way is to capture the input as a string and then convert the string to an integer. The reason is that all standard input (regardless of where it comes from) is done through character streams and it's safer to capture this input using a string rather than trying to perform conversions on the incoming data directly.
"Product" is a binary operator. A binary operator takes two numbers as input and combines them into an output. Your question gives only one number as input and so a sensible answer is impossible. "Product" is a binary operator. A binary operator takes two numbers as input and combines them into an output. Your question gives only one number as input and so a sensible answer is impossible. "Product" is a binary operator. A binary operator takes two numbers as input and combines them into an output. Your question gives only one number as input and so a sensible answer is impossible. "Product" is a binary operator. A binary operator takes two numbers as input and combines them into an output. Your question gives only one number as input and so a sensible answer is impossible.
std::string input = ""; std::getline (std::cin, input); // get input from stdin std::stringstream ss (input); // place input in a string stream integer num = 0; if (ss >> num) // extract integer from string stream { // Success! } else { // Fail! }
Console.WriteLine("Please input a string:"); string str = Console.ReadLine(); Console.WriteLine("Number of characters: " + str.Length);
#include<iostream> #include<string> int compare (const std::string& a, const std::string& b) { int n=1; std::string::const_iterator ai=a.begin(), bi=b.begin(); for (int n=1; ai!=a.end() && bi!=b.end(); ++ai, ++bi, ++n) { char ca = *ai; char cb = *bi; if (ca<cb) return n * (-1); if (cb<ca) return n; } if (ai==a.end() && bi==b.end()) return 0; if (ai==a.end()) return n * (-1); return n; } int main() { std::cout << "When comparing strings, zero indicates the two strings are equal.\n" << "A negative value indicates the first string is less than the second string.\n" << "A positive value indicates the first string is greater than the second string.\n" << "The absolute value indicates the characters that differ in the strings. Thus\n" << "-10 or 10 indicate that the 10th characters show a difference.\n" << std::endl; std::string x = "This is a string."; std::string y = "This is another string."; std::string z = "This is another."; std::cout << "String x = "" << x << """ << std::endl; std::cout << "String y = "" << y << """ << std::endl; std::cout << "String z = "" << z << """ << std::endl; std::cout << std::endl; std::cout << "compare(x,x) = " << compare(x,x) << std::endl; std::cout << "compare(x,y) = " << compare(x,y) << std::endl; std::cout << "compare(x,z) = " << compare(x,z) << std::endl; std::cout << "compare(y,x) = " << compare(y,x) << std::endl; std::cout << "compare(y,y) = " << compare(y,y) << std::endl; std::cout << "compare(y,z) = " << compare(y,z) << std::endl; std::cout << "compare(z,x) = " << compare(z,x) << std::endl; std::cout << "compare(z,y) = " << compare(z,y) << std::endl; std::cout << "compare(z,z) = " << compare(z,z) << std::endl; std::cout << std::endl; }
#include<iostream> #include<string> #include<sstream> unsigned input_num (std::string prompt) { unsigned id = 0; while (1) { std::cout<<prompt<<": "; std::string input=""; getline (std::cin, input); std::stringstream ss (input); if (ss>>id) break; std::cout<<"Invalid input.\n"; } return (id); } char input_op (std::string ops) { char op = 0; while (1) { std::cout<<"Enter an operator ("<<ops<<"): "; std::string input=""; getline (std::cin, input); std::stringstream ss (input); if (ss>>op && ops.find (op) != std::string::npos) break; std::cout<<"Invalid input.\n"; } return (op); } int main() { unsigned num1 = input_num ("Enter a number"); unsigned num2 = input_num ("Enter another number"); // division is invalid if num2 is zero char op = input_op (num2?"+-*/":"+-*"); unsigned result = 0; switch (op) { case ('+'): result = num1+num2; break; case ('-'): result = num1-num2; break; case ('*'): result = num1*num2; break; case ('/'): result = num1/num2; } std::cout<<num1<<op<<num2<<'='<<result<<std::endl; }
import java.util.*; public class Example { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Please enter a string:"); String input = in.next(); System.out.println("The String you entered is: " + input); } }
#include<iostream> #include<string> #include<sstream> #include<vector> using namespace std; class student { friend ostream& operator<< (ostream&, const student&); private: static size_t id; string m_id; string m_forename; string m_surname; public: static string generate_id(); student (const string& forename, const string& surname) : m_id (generate_id()), m_forename (forename), m_surname (surname) {} }; size_t student::id {0}; string student::generate_id() { stringstream ss; ss << ++id; string student_id; ss >> student_id; while (student_id.size() < 4) student_id.insert (student_id.begin(), '0'); return "STU" + student_id; } ostream& operator<< (ostream& os, const student& stud) { os << stud.m_id << '\t' << stud.m_forename << ' ' << stud.m_surname; return os; } size_t input_number (const string& caption) { while (true) { cout << caption << ": "; string input; cin >> input; stringstream ss; ss << input; size_t num; if (ss >> num && 0<num) return num; cerr << "Invalid input. Please try again." << endl; } } string input_string (const string& caption) { cout << caption << ": "; string input; cin >> input; return input; } int main() { vector<student> students; cout << "Student entry system\n" << endl; size_t num_of_students = input_number ("Number of students"); size_t count = 0; while (count++ != num_of_students) { cout << "Student #" << count << endl; string fname = input_string ("Enter forename"); string sname = input_string ("Enter surname"); students.push_back (student (fname, sname)); } cout << endl; for (auto s : students) cout << s << endl; }
The charAt function returns the number of occurrences of a specified character in the input string.
Scanner scan = new Scanner("[input method]"); // input method could they key board //(System.in) or a String String one = scan.next() + scan.next(); // get the first 2 words and concatenate String two = scan.next(); // the remaining word
There are two stream operators: << (insert or put) and >> (extract or get). Output streams implement the insertion operator, input streams implement the extraction operator and input/output streams implement both operators.