C++ ? Why the value does not change ? [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I just learned how to use this operator : ->,
and I am trying to create practice programs so I can remember it and be familiar with it.
I created a program that inputs my health and then heal my (add health) using the -> operator.
But when I run program, my health stays at 50 (cause I set my current health to 50).
here is my code :
#include <iostream>
using namespace std;
struct myhealth
{
unsigned short my_health;
};
void addhealth(myhealth* addhealth)
{
addhealth->my_health += 50;
};
int main()
{
myhealth player;
player.my_health = 50;
cout << "My earlier health : " << player.my_health << endl;
myhealth();
cout << "My current health : " << player.my_health;
cin.get()
return 0;
}

You never call addhealth and so the value is never modified.
This line of code:
myhealth();
appears to have been written in error. Instead I think you meant to write:
addhealth(&player);

Related

compiler issues with simple array program that wont build due to limitations in scope perhaps [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
this is my first question on stack overflow.. Im having compiler issues and wanted to figure out if anyone could help me find out why.
this is the unfortunate error message I recieve every time I attempt to compile.
//*************************************************************************
// This program uses array with a for loop control structure to prompt the user to enter their most convenient days of the week to work
#include <iostream>
using namespace std;
#include <array>
#include <cstddef>
int main() {
int whichDay = 0;
array< string, 7> daysOfweek =
{
"sunday",
"monday",
"tuesday",
"wednesday",
"thursday",
"Friday",
"saturday"
};
for (size_t x= 0; x< daysOfweek.size(); x++)
{
cout << daysOfWeek[x] << endl;
};
cout << "enter your work day: " ;
cin >> whichDay;
cout << daysOfWeek[whichDay] += 1
return 0;
}
This is your issue: cout << daysOfWeek[whichDay] += 1.
What would you expect the result of "monday" += 1 to be?
You probably just want to display the chosen day, so you just need:
cout << daysOfWeek[whichDay];
Well, you also may want to check that the input value is in the valid range :)

Set is not removing the duplicates [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am having trouble putting a file of words into a set. I can read the file and the words go into the set but the set doesn't discard the repeated words. Here is the snippet of code that I believe is causing the problem.
using namespace std;
while(readText >> line){
set<string> wordSet;
wordSet.insert(line);
for (std::set<std::string>::iterator i = wordSet.begin(); i != wordSet.end(); i++)
{
cout << *i << " ";
}
}
the sample file is this
1
2
2
3
4
5
5
and the output is exactly the same
As stated in comments, you are not using the std::set correctly. You need to move it, and the for loop, outside of your while loop:
using namespace std;
set<string> wordSet;
while(readText >> line) {
wordSet.insert(line);
}
for (set<string>::iterator i = wordSet.begin(); i != wordSet.end(); i++) {
cout << *i << " ";
}

Use for loop to write to many files in c++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying to write data to a 100 files gnu_2D_t0_Q_xt_2sol_anal.dat, gnu_2D_t1_Q_xt_2sol_anal.dat, ..., gnu_2D_t100_Q_xt_2sol_anal.dat. Using this question as inspiration, I've come up with the following snippet of code
int m = 100;
for(int k=0;k<m;k++)
{
stringstream aa; // http://www.dreamincode.net/forums/topic/95826-stringstream-tutorial/
aa << k;
filename = "gnu_2D_t" + aa.str() + "_Q_xt_2sol_anal.dat";
files.open(filename.c_str()); // http://www.cplusplus.com/forum/beginner/122208/
for(int i=0;i<SPACE;i=i+1)
{
files << x_begin + (i * h) << setw(18);
files << Q_xt_matrix[i][t_matrix[m]] << setw(18);
files << endl;
}
files.close;
}
However, it yields the following error
error: invalid use of non-static member function files.close;
^
Does anybody what is wrong with my code?
close is a method, not a member of files. Change the last line to files.close().

Why do i get this wrong result? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'M trying to make a code that is able to increase int 'd' by one each time the a+b reach 20.
and if there still any number less than 20 then this will be the int 'c'.
but instead of getting the right result in my next program which is
49-0
i get this wrong answer
47-40
what should i do ?
#include <iostream>
using namespace std;
int main(){
int a=50;
int b=18;
int c=a+b;
int d=0;
int i;
for(i=0;i<c;i++)
{
while(c>20)
{
d+=1;
c=c-20;
break;
}}
cout<<d<<"-"<<c;
return 0;
}
The problem is in your while loop:
while(c > 20)
{
d+=1;
c=c-20;
break;
}
The loop will only execute once because of your break statement.

how to get correct answer merge 2 sorted arrays?! C++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
i wrote a little algorithm for marge to sorted array. but i have problem with that.
#include <iostream>
using namespace std;
int main() {
// main function started form here:
int firstArray[10] = {1,3,5,7,9,11,13,15,17,19};
int secondtArray[10] = {2,4,6,8,10,12,14,16,18,20};
int mergedArray[20];
int firstCounter=0 , secondtCounter=0 , mergedCounter=0;
while(firstCounter < 10 && secondtCounter < 10){
if(firstArray[firstCounter] < secondtArray[secondtCounter]){
mergedArray[mergedCounter] = firstArray[firstCounter];
firstCounter++;
} else {
mergedArray[mergedCounter] = secondtArray[secondtCounter];
secondtCounter++;
}
mergedCounter++;
}
while(firstCounter < 10) {
mergedArray[mergedCounter] = firstArray[firstCounter];
firstCounter++;
mergedCounter++;
}
while(secondtCounter < 10) {
mergedArray[mergedCounter];
secondtCounter++;
mergedCounter++;
}
for(int j=0; j<20; j++){
//cout << mergedArray[j] << endl;
}
cout << mergedArray[19];
return 0;
}
in outpout for array mergedArray[19] i get something like this: 2686916!!!
i don't know why i get this value. how can i fix that. and why i get this value.
Typo in last while. You may increase your warning level to let your compiler show you your typo (warning: statement has no effect [-Wunused-value]).
while(secondtCounter < 10) {
mergedArray[mergedCounter];
secondtCounter++;
mergedCounter++;
}
should be
while(secondtCounter < 10) {
mergedArray[mergedCounter] = secondtArray[secondtCounter];
secondtCounter++;
mergedCounter++;
}
As pointed out by WhozCraig's comment, you're not assigning any value to mergedArray[19] because you left out the assignment part of the statement.
Since you haven't assigned a value, it's printing out whatever value happens to be at that memory address from previous usage. If you run your program (as it's currently written) several times, you'll see that the number there might change. Also, if you'd printed out the values in mergedArray before assigning anything, you'd see more such meaningless (to you in the current application) numbers.