Difficulty understanding while loop execution - c++

#include <iostream>
#include <iomanip>
#include <ios>
#include <algorithm>
#include <iomanip>
#include <string>
#include <vector>
using std::cin; // <iostream>
using std::cout; // <iostream>
using std::endl; // <iostream>
using std::setprecision; // <iomanip>
using std::sort; // <algorithm>
using std::streamsize; // <ios>
using std::string; // <string>
using std::vector; // <string>
int main()
{
cout << "Enter your homework grades : " << endl;
double x;
vector<double> homework;
int count = 0;
while(cin >> x)
{
homework.push_back(x);
++count;
if(count == 0)
{
cout << "Error, enter a grade" << endl;
continue;
}
}
return 0;
}
Hi, I'm wondering why my while loop wont print the message (Error, enter a grade) on the screen if the if-statement is within the loop, and seems to only work when its placed outside after the loop executes why is this the case?

You are never going back to zero.
int count = 0;
//...
while(cin >> x)
{
homework.push_back(x);
++count;
if(count == 0) //count will never be zero

Related

why my cpp code can't run?(about char*[])

this is my code
the error is Segmentation fault,and i can't understand why
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
int main(int argc, char* argv[])
{
char* szword[100];
int i = 0;
do
{
cin >> szword[i];
cout << szword[i];
i++;
}while(strcmp(szword[i - 1], "done"));
cout << i + 1;
return 0;
}
For starters neither declaration from headers <cstdio> and <string> is used in your program. So you should remove these directives
#include <cstdio>
#include <string>
You declared an initialized array with the element type char *. Thus this statement
cin >> szword[i];
invokes undefined behavior because the pointer szword[i] has indeterminate value.
Moreover this call even if the argument of the operator will be correct
cin >> szword[i];
can fail. You should check whether it was successful. And I think there is no great sense to output the string "done".
Also in this statement
cout << i + 1;
you are outputting a value that is greater than the number of inputted strings.
If to use character arrays then your program could look the following way
#include <iostream>
#include <cstring>
int main()
{
const size_t N = 100;
char szword[N][N];
size_t i = 0;
while ( std::cin.getline( szword[i], sizeof( szword[i] ) ) &&
std::strcmp( szword[i], "done" ) != 0 )
{
std::cout << szword[i++] << '\n';
}
std::cout << i << '\n';
return 0;
}
The program output might look like
Hello
World
2
This below code works fine, if you want to use char *, for C++ string you can use the C++ version
C Version:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
int main(int argc, char* argv[])
{
char *tmp;
int i = 0;
do
{
cin >> tmp;
cout << tmp;
i++;
}while(strcmp(tmp, "done"));
cout << i + 1;
return 0;
}
C++ Version:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
int main(int argc, char* argv[])
{
string tmp;
int i = 0;
do
{
cin >> tmp;
cout << tmp;
i++;
}while(tmp != "done"));
cout << i + 1;
return 0;
}

Printing birthdates using maps and they end up as Octals

If I create a map for names and birthdays. When i enter birthdates that end in 0 it changes it the number to an octal. how do i print out the birthday ie 010525 == 4437 so when i call it -> second it will print 010525
#include <stdio.h>
#include <iterator>
#include <string>
#include <map>
#include <iostream>
using namespace std;
int main(){
map<string, int>birth;
birth["Chicken"] = 010525;
birth["Dragon"] = 010266;
map<string, int>::iterator it;
for(it = birth.begin() ; it != birth.end(); it++){
cout << it -> first + " ";
cout << it ->second << endl;
}
}
Output:
Chicken 4437
Dragon 4278
There are 2 ways to fix this problem:
(1) The first way is to enter the birthday without the leading 0 as follow:
birth["Chicken"] = 10525;
birth["Dragon"] = 10266;
Then use setfill() and setw() to fill the leading 0.
Please note that the code that uses std:setw() and std::setfill() will compile only with new C++ compiler versions such as C++14 (or C++11)
#include <stdio.h>
#include <iterator>
#include <string>
#include <map>
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
map<string, int>birth;
birth["Chicken"] = 10525;
birth["Dragon"] = 10266;
map<string, int>::iterator it;
for(it = birth.begin() ; it != birth.end(); it++){
cout << it -> first + " ";
cout << setfill('0') << setw(6) << it ->second << endl;
}
}
(2) The second way is to store the birthday as string. This way you can enter the birthday with the leading 0.
Here is the code:
#include <stdio.h>
#include <iterator>
#include <string>
#include <map>
#include <iostream>
using namespace std;
int main(){
map<string, string>birth;
birth["Chicken"] = "010525";
birth["Dragon"] = "010266";
map<string, string>::iterator it;
for(it = birth.begin() ; it != birth.end(); it++){
cout << it -> first + " ";
cout << it ->second << endl;
}
}
You can go with using both as string in map like :
map<string, string>birth;
and rest keep as it is.

Error while performing string copy operation in C++

Please explain why is this giving an error but the other on is running fine
The following code gives the error:
#include <iostream>
#include <string>
#include <conio.h>
#include <math.h>
#include <iomanip>
#include <string.h>
using namespace std;
int main()
{
string s1,s2;
int i;
cout << "Enter the string to copy into another string : ";
getline(cin,s1);
for(i=0; s1[i]!='\0'; ++i)
{
s2[i]=s1[i];
}
s2[i]='\0';
cout<<"\n\nCopied String S2 is : "<<s2;
return 0;
}
Error looks like this
But this works perfectly fine
#include <iostream>
#include <string>
#include <conio.h>
#include <math.h>
#include <iomanip>
#include <string.h>
using namespace std;
int main()
{
char s1[100], s2[100], i;
cout << "Enter the string to copy into another string : ";
cin>>s1;
for(i=0; s1[i]!='\0'; ++i)
{
s2[i]=s1[i];
}
s2[i]='\0';
cout<<"\n\nCopied String S2 is : "<<s2;
return 0;
}
In your case, s2 is initialized to an empty string with a length of 0, so you can't write past the bounds. If you want to, you must first resize it:
s2.resize(s1.length());
for(i=0; s1[i]!='\0'; ++i)
{
s2[i]=s1[i];
}
Also, c++ std::string does not need a terminating nullbyte, unlike C strings.

C++ Error: No Member in Class?

I'm working on C++, and this is just a very basic program, but I'm still getting an error.
The error message is:
'class secondary' has no member named 'getting'.
Why is this? It works for my void setting, but not for getting? What am I doing wrong here?
main.cpp
#include <iostream>
#include <string>
#include "secondary.h"
using namespace std;
int main(){
secondary s;
int scale;
cout << "On a scale of 1-10, how awesome are you?" << endl;
cin >> scale;
cout << endl;
s.setting(scale);
cout << s.getting();
return 0;
}
secondary.h
#ifndef SECONDARY_H
#define SECONDARY_H
#include <string>
class secondary
{
public:
void setting(int x);
string getting();
};
#endif // SECONDARY_H
secondary.cpp
#include "secondary.h"
#include <iostream>
#include <string>
using namespace std;
void secondary::setting(int x){
factor = x;
}
string secondary::getting(){
string result;
if(factor < 3){
result = "You have a very low self esteem.";
}elseif(factor > 3){
if(factor > 7){
result = "You have a very high self esteem."
}else{
result = "You have a medium self esteem."
}
}
return result;
}
private factor;
Actually, looking at this again, and deeper, this code has many issues (semicolons missing at key points and the private int definition should have been in the header file, not the cpp file 9t(private is its own section, see below):The problem, from what I can see, s has not yet been instantiated yet, do so and the operation should work correctly.
Please also note that when factor was defined in the cpp file, it was defined at bottom, it should actually be defined before any use of the variable to be defined (in the header file is better meet with common/conventional coding standards).
Please check this tested code:
main.cpp
#include <iostream>
#include <string>
#include "secondary.h"
using namespace std;
int main(){
secondary s;
int scale;
cout << "On a scale of 1-10, how awesome are you?" << endl;
cin >> scale;
cout << endl;
s.setting(scale);
cout << s.getting();
return 0;
}
secondary.h
#ifndef SECONDARY_H
#define SECONDARY_H
#include <string>
class secondary
{
public:
void setting(int x);
std::string getting();
private: // Note: this is how you do private
int factor; // This is the definition with type int, missing in original
};
#endif // SECONDARY_H
secondary.cpp
#include "secondary.h"
#include <iostream>
#include <string>
using namespace std;
void secondary::setting(int x){
factor = x;
}
string secondary::getting(){
string result;
if (factor < 3){
result = "You have a very low self esteem.";
}else if(factor > 3){
if (factor > 7){
result = "You have a very high self esteem.";
}
else{
result = "You have a medium self esteem.";
}
}
return result;
}

how can i save a text file without typing ".txt" at the end?

My Program (c++):
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
float x, y, z;
char d[20];
int main()
{
cin.getline >>d;
x=111;
y=222;
z=333;
ofstream meuarquivo;
meuarquivo.open (d".txt");
meuarquivo << x << "\n";
meuarquivo << y << "\n";
meuarquivo << z << "\n";
meuarquivo.close ();
return 0;
}
I want to write something like "ThatsMyProgram", and I want the program to save this file as "ThatsMyProgram.txt". How can I do that?
Use std::string, which defines operator + for concatenation:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
int main()
{
std::string filename;
std::getline(std::cin, filename);
std::ofstream file((filename + ".txt").c_str());
// use stream here.
}