for-loop construction start in a c++ program [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a problem in the for-construction, I don't know why my counting starts from 0 but you can't write on it(you can write starting with Register 1).I've marked in the code where is the problem.
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::string day, date;
int registerCount;
std::cout << "INPUT DATA"
<< std::endl << std::endl
<< "Enter the day in which you want to perform the register: "
<< std::endl;
std::cin >> day;
std::cout << "DATE:" << std::endl;
std::cin >> date;
std::cout << "Enter the number of registers you wanna perfom for the day "
<< day << ":" << std::endl;
std::cin >> registerCount;
std::vector<std::string> registers(registerCount);
for (int i = 0; i < registerCount; ++i) **here is the problem**
{
std::cout << "Register " << i << ":" << std::endl; **it starts from 0 but you can't write on Register 0**
std::getline(std::cin, registers[i]);
}
std::cout << "The data for the day of " << day << " are the following: "
<< std::endl;
std::cout << "DATE: " << date << std::endl;
for (int i = 0; i < registerCount; ++i)
std::cout << registers[i] << std::endl;
}

Use
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Header - #include<limits>
after your
std::cin >> registerCount;
to "eat up" the trailing newline coming from previous std::cin which is cause of skipping of the first input

for (int i = 0; i < registerCount; ++i)
You're defining i as starting at 0 here. Very simply, all you need to do is change the "i = 0" to "i = 1".
The way you're assigning to that vector looks a little bit wrong too. I think you need to use push_back() on the vector in that syntax, but I'm not too familiar with all the different operators for vector.

Related

C++ Calling a string from a struct stops the program [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 2 years ago.
Improve this question
I'm a beginner in C++. I appreciate any suggestions in which could improve my code but what I'm really looking for is an explanation as to why my current code isn't returning what I want.
#include <iostream>
#include <string>
using namespace std;
struct MATRIX {
float values[1][1];
string names;
};
MATRIX get_matrix(string name, MATRIX m){
m.names = name;
cout << "Enter values of 2x2 Matrix:" << endl;
cin >> m.values[0][0];
cin >> m.values[0][1];
cin >> m.values[1][0];
cin >> m.values[1][1];
return m;
}
// end get_matrix1
MATRIX get_matrix2(string name2, MATRIX m2){
m2.names = name2;
cout << "Enter values of 2x2 Matrix:" << endl;
cin >> m2.values[0][0];
cin >> m2.values[0][1];
cin >> m2.values[1][0];
cin >> m2.values[1][1];
return m2;
}
int main(){
string testname;
MATRIX matrixtest;
string testname2;
MATRIX matrixtest2;
cout << "Name 1st Matrix:" << endl;
cin >> testname;
MATRIX result = get_matrix(testname, matrixtest);
cout << "Name 2nd Matrix:" << endl;
cin >> testname2;
MATRIX result2 = get_matrix2(testname2, matrixtest2);
cout << "[" << result.values[0][0] << "," << result.values[0][1] << "]" << endl;
cout << "[" << result.values[1][0] << "," << result.values[1][1] << "]" << endl << endl;
cout << "[" << result2.values[0][0] << "," << result2.values[0][1] << "]" << endl;
cout << "[" << result2.values[1][0] << "," << result2.values[1][1] << "]" << endl << endl;
}
It's supposed to return the name of the matrix as well as its values. Whenever it hits the name member in result it just stops the program.
EDIT: Updated to current code
You have to specify the number of elements, not the maximum index, to declare arrays in C++.
With this declaration
float values[1][1];
Only values[0][0] is avaliable.
The declaration should be
float values[2][2];

How to create a valid write to a binary file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have created student data to be stored in a binary file but this file is completely broken.
What am I doing wrong?
there was no problem with the text file stored in the notepad.
now after entering after starting the program, it only writes this:
& Ô LC_CTYPE = C; LC_M0 & Ô RIC = C; LC_TIME = C
Is there someone able to help me with this? Have a nice day:)
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
struct Student{
string imie;
string nazwisko;
int nrAlbumu;
int wiek;
float srOcen;
} dane;
int main(){
Student dane;
cout << "Podaj imie:" << endl;
cin >> dane.imie;
cout << "Podaj nazwisko:" << endl;
cin >> dane.nazwisko;
cout << "Podaj nrAlbumu:" << endl;
cin >> dane.nrAlbumu;
cout << "Podaj wiek:" << endl;
cin >> dane.wiek;
cout << "Podaj srednia ocen:" << endl;
cin >> dane.srOcen;
cout << "Student " << dane.imie <<" "<< dane.nazwisko << " o numerze albumu: " << dane.nrAlbumu << " ma lat " << dane.wiek << " ma srednia ocen rowna: " << dane.srOcen << endl;
//-------writing to the file starts here-------------------------
ofstream ofs("dane.bin", ios::binary);
Student* student = new Student;
student->imie;
student->nazwisko;
student->nrAlbumu;
student->wiek;
student->srOcen;
ofs.write((char*)(student), sizeof(Student));
ofs.close();
delete student;
//-------------reading starts here ---------------------------
ifstream ifs("dane.bin", ios::binary);
char* temp = new char[sizeof(Student)];
ifs.read(temp, sizeof(Student));
Student* student2 = (Student*)(temp);
cout << "Student " << dane.imie <<" "<< dane.nazwisko << " o numerze albumu: " << dane.nrAlbumu << " oraz ma lat " << dane.wiek << " ma srednia ocen rowna: " << dane.srOcen <<" Potwierdzenie do zapisu i odczytu!" << endl;
delete student;
return 0;
}
The Student object contains std::string members. That is an object that is not trivially-copyable, thus the Student class is not trivially-copyable, so you cannot simply read and write this type to a binary file by merely copying the bytes to and from the file.
If you want more proof, what does sizeof do? What value does it return? The sizeof is a compile-time value. Now let's say that std::string has a size() of 1000 characters. The sizeof had no idea how many characters at runtime the std::string will have. Thus that entire line makes no sense and will not work.
If you did the following, given that there would be 1000 characters in one of the std::string members:
std::cout << sizeof(Student);
what value do you get? I bet it isn't 1000 or greater.
Here is a quick program illustrating the problem:
#include <type_traits>
#include <string>
#include <iostream>
struct Student{
std::string imie;
std::string nazwisko;
int nrAlbumu;
int wiek;
float srOcen;
};
struct Student2{
char imie[20];
char nazwisko[20];
int nrAlbumu;
int wiek;
float srOcen;
};
int main()
{
Student s;
s.imie = std::string(1000, ' ');
std::cout << "The number of characters is: " << s.imie.size() << "\n";
std::cout << "The sizeof(Student) is: " << sizeof(s) << "\n";
std::cout << "Is Student trivially copyable? " << (std::is_trivially_copyable<Student>()?"Yes":"No") << "\n";
std::cout << "Is Student2 trivially copyable? " << (std::is_trivially_copyable<Student2>()?"Yes":"No") << "\n";
}
Output:
The number of characters is: 1000
The sizeof(Student) is: 80
Is Student trivially copyable? No
Is Student2 trivially copyable? Yes
Note that if you used char arrays instead of std::string, then the struct becomes trivially copyable and then can be used to write to a binary file in the way you're currently doing it now.
However the disadvantage is that you are limited to the number of characters (20 in my example). If you still want to use std::string, you have to write the struct in individual pieces, taking care of each std::string accordingly as specified by this answer.

Learning about file input and output, I'm able to write to file but I can't print from file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 days ago.
Improve this question
I'm trying to write a simple file input and output program called employee list. It creates the list and stores the names and the salaries of the employees entered in a list.
The first problem I have is, anytime I run the code, it just replaces the list instead of adding to it.
My second problem is printing from the file to the console, It just prints a negative integer.
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
int main() {
std::string First_Name;
std::string Last_Name;
std::string NAMES;
int SALARY;
int Employee_Salary;
std::fstream Employee_List("employee list.txt",
std::ios::in | std::ios::out | std::ios::app);
std::cout << std::left << std::endl;
if (!Employee_List) {
std::cout << "File not Found" << std::endl;
return -1;
} else {
Employee_List << "*************Employee Names and Salary***************"
<< std::endl;
Employee_List << "Names" << std::setw(25) << "Salary" << std::endl;
std::cout << "Please Enter First Name" << std::endl;
std::cin >> First_Name;
std::cout << "Please Enter Last Name" << std::endl;
std::cin >> Last_Name;
std::cout << "Please Enter Salary" << std::endl;
std::cin >> SALARY;
std::cout << std::endl << std::endl << std::endl;
Employee_List << First_Name << " " << Last_Name << "," << std::setw(18)
<< SALARY << std::endl;
Employee_List.ignore(255, '\n');
std::getline(Employee_List, NAMES, ',');
Employee_List >> Employee_Salary;
std::cout << std::setw(20) << NAMES << Employee_Salary << std::endl;
while (!Employee_List.eof()) {
std::cout << std::setw(20) << NAMES << Employee_Salary << std::endl;
std::getline(Employee_List, NAMES, ',');
Employee_List >> Employee_Salary;
}
}
}
I've had this issue. (1st Issue) What you are currently doing is replacing the file contents every time you write something to it. What you want to be doing is appending to the file. Check out this link:
Append to a File with fstream Instead of Overwriting

C++ int x++ and int ++x not working how I thought they would? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
I have a simple program but it's running weirdly. Basically the code runs fine but when the numbering at the beginning of the line comes into play, int x++ displays the same number as the first line then continues. Why does this happen?
Code:
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <string>
#include "logo.h"
int main()
{
SetConsoleTitle("plains.exe");
displayLogo();
int number;
int addTotal = 0;
int numbersEntered = 0;
std::cout << " [1] enter your first number: ";
std::cin >> number;
while (number != -1) {
addTotal = addTotal + number;
numbersEntered++;
std::cout << " [" << numbersEntered << "]" << " enter your next number or type '-1' to add them: ";
std::cin >> number;
}
if (number == -1) {
std::cout << " " << std::endl;
std::cout << " --------------------------------" << std::endl;
std::cout << " " << std::endl;
std::cout << " the sum of your numbers is " << addTotal << "." << std::endl;
std::cout << " you entered a total of " << numbersEntered << " numbers." << std::endl;
std::cout << " " << std::endl;
std::cout << " the average of your numbers is " << addTotal / numbersEntered << "." << std::endl;
std::cout << " " << std::endl;
}
return 0;
}
You initialized numbersEntered to 0. The first time through the while loop, it does numbersEntered++, which sets it to 1. So the first prompt in the loop contains [1]. This is the same as what you printed before the loop with:
std::cout << " [1] enter your first number: ";
so you see [1] twice.
To prevent this duplication, add 1 to the variable when displaying the prompt:
std::cout << " [" << (numbersEntered++) << "]" << " enter your next number or type '-1' to add them: ";

Alternative way to push into the stack [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I developed this program to add and the retrieve 15$ in the stack.
I was wondering is there another more efficient way to write my code.
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int main()
{
const int MAX = 100;
int count;
stack<int, vector<int> > billStack;
for (int i=0; i<15; i++) {
billStack.push(i); // add 15 bills onto stack
}
cout << "The stack has " << billStack.size() << " bills.\n";
int cash;
cout << "How many bills to retrieve?\n";
cin >> cash;
for (int i = 0; i< cash; i++) {
billStack.pop();
}
cout << "Cash out :" << cash << ". Remaining: " << billStack.size() << endl;
return 0;
}
Considering that you don't ever use the actual contents of the stack, just its size:
#include <iostream>
using namespace std;
int main()
{
int stackSize = 15;
cout << "The stack has " << stackSize << " bills.\n";
int cash;
cout << "How many bills to retrieve?\n";
cin >> cash;
stackSize -= cash;
cout << "Cash out :" << cash << ". Remaining: " << stackSize << endl;
return 0;
}