CODE ONE
#include
#include
using namespace std;
string x =" I am global"; // Global x
int main()
{
string x = " I am local"; // Local x
cout< cout<<::x<
return 0;
}
CODE TWO
#include
#include
using namespace std;
string str = "i am global ";// global
int main()
{
string srt = "i am local to main() ";//local to main()
cout << str << "---" << ::str << "\n";//
LINE 5555.
int x=5;
int y=6;
if (y>5)
{
string str ="i am local to if block";//local to " if " block
statement
cout << str<< "---" << ::str<< "\n";//
LINE 6666 prints "i am local to if block", and "i
am global", respectively.
return 0;
}
Hello. I have a question about the two given codes above.
RULE 1
From what I understand, if we have global and local variables with the SAME name, preference ( operation like cout<) is given to the local variable instead of the global one with the assumption that local variable is inside the scope of some functions such as main().
This is true for CODE ONE. To print the global variable, we can use the scope resolution operator: : as shown in CODE ONE. The output is "I am local --- I am global".
RULE 2
For sub-block like "if", the rule is this = the scope resolution operator will give preference ONLY to the global variable, not to the local variable inside the main(), " I am local to main ". as shown in CODE TWO.
However, in CODE TWO, LINE 5555, the result seems to contradict rule RULE 1.
LINE 5555) prints " i am global ---- i am global ". I was expecting it to print "i am local to main --- i am global".
Line 6666 CODE TWO prints "i am local to if block --- i am global".
WHY THE OUTPUT IS NOT "i am local to main --- i am global". since "i am local to main" is declared inside the main function ????
THANK YOU IN ADVANCE FOR ANY HELPFULL EXPLANATION.
Because your
String variable is srt and NOT str
string srt = "i am local to
main() ";//local to main()
When I change srt to str,It gives
correct output and your understanding is CORRECT..
SEE IMAGE,
PLEASE COMMENT if there is any concern.
==============================
CODE ONE #include #include using namespace std; string x =" I am global"; // Global x...
#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() { char string[100]; char inputChoice, choice[2]; int vowelTotal, consonantTotal; //Input a string cout << "Enter a string: " << endl; cin.getline(string, 100); do { //Displays the Menu cout << " (A) Count the number of vowels in the string"<<endl; cout << " (B) Count...
#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() { char string[100]; char inputChoice, choice[2]; int vowelTotal, consonantTotal; //Input a string cout << "Enter a string: " << endl; cin.getline(string, 100); do { //Displays the Menu cout << " (A) Count the number of vowels in the string"<<endl; cout << " (B) Count...
***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName) { cout << "Usage: " << executableName << " [-c] [-s] string ... " << endl; cout << " -c: turn on case sensitivity" << endl; cout << " -s: turn off ignoring spaces" << endl; exit(1); //prints program usage message in case no strings were found at command line } string tolower(string str) { for(unsigned int i = 0; i < str.length(); i++)...
what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...
Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std; int main(){ int rows = 5; int cols = 5; int x; int** arr = new int[rows][cols] cin >> x; arr[x][x] = x; cout << "arr[x][x] = " << arr[x][x]; return 0; } Question 2: Fix the code to initialize the 2D array elements to x #include <iostream> using namespace std; int main(){ int rows; int cols; int x;...
Find Output. Just output. No explanation needed.. #include <iostream> #include <string> using namespace std; class baseClass { public: void print() const; baseClass(string s = " ", int a = 0); //Postcondition: str = s; x = a; protected: int x; private: string str; }; class derivedClass: public baseClass { public: void print() const; derivedClass(string s = "", int a = 0, int b = 0); //Postcondition: str = s; x = a; y = b; private: int y; }; int...
#include <iostream> #include <sstream> #include <string> using namespace std; int main() { const int index = 5; int head = 0; string s[index]; int flag = 1; int choice; while (flag) { cout << "\n1. Add an Item in the Chores List."; cout << "\n2. How many Chores are in the list."; cout << "\n3. Show the list of Chores."; cout << "\n4. Delete an...
C++ assignment. Please help me to complete this code: #include <string> #include <iostream> using namespace std; // Write your function here //////////////// STUDENT TESTING //////////////////// int run() { cout << "Student testing" << endl; strip(); return 0; } Here is the instruction: Write a filter function named strip that removes C++ comments from input, sending the uncommented portion of the program to output. Your program should work with both single line (//) comments, and multi-line (/* */) comments. + A...
#include <iostream> #include <string> using namespace std; //Write a function that changes all characters in a string to dashes string to_dash(string s){ for(int i = 0; i < s.length(); i++){ } return s; } int main(){ string s; cin >> s; s = to_dash(s); cout << s << endl; }
4) What is the output if the input istom - Sawyer? #include <iostream> using namespace std; int main() { string playerName; cout << "Enter name"; cin >> playerName; cout << endl « playerName; return 0; } a. Tom - Sawyer b. Tom Sawyer c. Tom d. Sawyer 5) Which XXX generates "Adam is 30 years old." as the output? #include <iostream> using namespace std; int main() { string name = "Adam"; int age = 30; XXX return 0; } a....