This question already has answers here:
why doesn't C++ allow rebinding a reference?
(7 answers)
Can we reassign the reference in C++?
(7 answers)
Closed last month.
Here is my simple code
#include <iostream>
#include <string>
using namespace std;
int main() {
string food = "Pizza";
string &meal = food;
string okay = "okay";
&meal = okay; // showing error in updating meal reference
cout << food << "\n"; // Outputs should be Pizza
cout << meal << "\n"; // Outputs Should be okay
return 0;
}
I reference have a meal variable to the same location as food so now the meal value is Pizza but now I want to update the meal reference location to okay such that the food value is still Pizza but the meal updated to okay.
is there any way to update the reference variable to another variable?
Related
This question already has answers here:
How does std::flush work?
(4 answers)
Optimizing away a "while(1);" in C++0x
(9 answers)
Closed 3 years ago.
#include <iostream>
#include <string>
int main()
{
std::string name;
std::cout << "What is your name? ";
getline (std::cin, name);
std::cout << "Hello, " << name << "!\n";
while (true);
std::cout<<"over";
}
Program outputs “What is your name” and I input my name, say “Dom”, but the program then waits there. Why doesn’t it show “Hello Dom!” before it gets stuck at the while loop?
This question already has answers here:
Why in the code "456"+1, output is "56" [duplicate]
(3 answers)
Closed 6 years ago.
I am doing some exercises in C++ when I came upon something not so clear for me:
cout << "String" + 1 << endl;
outputs : tring
I suggest it is something with pointer arithmetic, but does that mean that everytime I print something in quotes that is not part of previous defined array,I actually create a char array ?
A quoted string (formally a string literal) is an array of const char, regardless of whether your printing it or doing anything else with it.
Code:
cout << "String" + 1 << endl;
has the same effect as this:
const char *ptr = "String";
cout << ptr + 1 << endl;
so no you do not create a new array, you just change pointer and pass it to std::cout
This question already has answers here:
Understanding pointers and local scope [duplicate]
(4 answers)
Closed 6 years ago.
I want to read from user input some numbers and then display them 5 on each line. My code is like this:
#include <iostream>
using namespace std;
const int INPUT_SIZE = 7;
void printValues(int *b) {
int counter = 0;
while (counter < INPUT_SIZE) {
cout << *(b+counter) << " ";
if ((counter + 1) % 5 == 0 && counter>0) {
cout << endl;
}
counter++;
}
cout << endl;
system("pause");
}
int * readValues() {
int b[INPUT_SIZE];
for (int i = 0; i < INPUT_SIZE; i++) {
cout << "Enter element on position: " << i << ": ";
cin >> b[i];
}
return b;
}
int main() {
int* b;
b = readValues();
printValues(b);
return 0;
}
However, when I try to print them, I get some weird numbers which I think are memory locations. How do I print an array and also how do I write a function that returns an array? I have little experience with pointers, as I only coded in Java so far.
Help much appreciated.
Your array, b, is a local variable inside the function readValues.
When readValues exits, the array is destroyed.
The pointer which you return from readValues is invalidated as soon as the function returns. Trying to use it (e.g. by passing it to printValues) is incorrect (formally, it causes undefined behaviour).
You may have caused some confusion by giving the same name b to a pointer variable in main as you do to the array b in readValues. They are entirely separate variables.
If you want to use the same array in your two functions, you need to make sure it lives in a scope which ensures it lives as long as you need it to. This could be by making it a local variable in main.
This question already has answers here:
How do you reverse a string in place in C or C++?
(21 answers)
Closed 7 years ago.
Hey guys I'm new here and a programming noob so bear with me here.
This is for my C++ class which sadly my teacher is terrible at teaching so many things confuse me so I need some help here.
We have a lab that is called 'Reverse Sentence' and this is what it wants In this lab.
Write the function "ReverseSentence" that takes a string parameter and changes it, by reversing it.
For example:
INPUT: the first test
OUTPUT: tset tsrif eht
The function must not use an extra string, but must reverse the elements of the input string.
#include <iostream>
#include <string>
using namespace std;
void ReverseSentence( string& inputSentence){
/* Body of Function Here */
}
int main(){
string inputSentence;
cout << "Input your sentence: ";
getline(cin, inputSentence);
cout << endl;
ReverseSentence(inputSentence);
cout << "Reversed Sentence:" << endl;
cout << inputSentence << endl;
return 0;
}
Can someone please help me what function is because I'm having trouble with it.
Just use std::reverse:
void ReverseSentence( string& inputSentence){
std::reverse(inputSentence.begin(), inputSentence.end());
}
Half of the cycle and swap.
#include<algorithm>
#include<string>
void ReverseSentence(std::string &s){
for (int i = 0; i < s.size()/2; ++i)
std::swap(s[i], s[s.size() - i - 1]);
}
This question already has answers here:
When I change a parameter inside a function, does it change for the caller, too?
(4 answers)
Closed 7 years ago.
I've been having a problem in c++ where I call a function which assigns some values to things, but those assignments are lost after the function has been completed. Here is my code:
#include <iostream>
#include <string>
using namespace std;
void Input(string a, string b){
cout << "Input a: \n";
cin >> a;
cout << endl;
cout << "Input b: \n";
cin >> b;
cout << endl << "Inputen Strings (still in the called function): \n";
cout << a << " " << b << endl << endl;
};
int main(){
string c = "This didn't";
string d = "work";
Input(c,d);
cout << "Inputen Strings (now in the main function): \n";
cout << c + " " + d << endl;
return 0;
};
So that whenever I run it, (inputting "Hello" and then "World") the program runs as follows:
Input a:
Hello
Input b:
World
Inputen Strings (still in the called function):
Hello World
Inputen Strings (now in the main function):
This didn't work
I don't know why it's only temporarily saving the values. Any help is appreciated!
change your method signature to accept the address of the variables "&"
void Input(string &a, string &b)
without the "&" operator you are just sending copy's of the variable into the function, with the "&" address-of operator you are passing the variables by reference
Pass your strings by reference and that will allow the called function to change them so the variables in the calling function will have the assigned values.
The way you have it passing by value now, you are just sending a copy of the variables so the new values are lost when Input returns.
void Input(string &a, string &b)
{
...
}