Before I ask anything, I should mention that I have a shaky foundation in C++. Let me know if I'm not being clear on anything and I will do my best to clarify.
My coding problem here is reading a series of 24-hour time values, with no seconds included, and storing them into an array of a structure. Reading the hours and minutes in integer format, and storing it into an array of structures is what I'm not understanding with this. In the text file, the first number in each row is the 24-hour time, and the second number is the amount of minutes that I need to modify the time with. I'm stuck frozen at just reading the times in to begin with.
This is the code I have so far.This is the result of the code.
#include <iostream>
#include <fstream>
using namespace std;
int main(){
int size = 7;
int i;
struct Times {
int T;
int M;
};
Times clock[7];
ifstream infile;
infile.open("times.txt");
for (i=0; i<size; i++){
infile>>clock[i].T>>clock[i].M;
}
for (i=0; i<size; i++){
cout<<clock[i].T << " "
<<clock[i].M <<endl;
}
}
Here is the contents of the text file:
6:45 39
12:00 73
0:00 4
23:59 1
22:45 70
11:59 1
14:15 95
Here is the updated code which seems to work:
#include <iostream>
#include <fstream>
using namespace std;
int main(){
int size = 7;
int i;
char colon;
struct Times {
int hour;
int minute;
int M;
};
Times clock[7];
ifstream infile;
infile.open("times.txt");
for (i=0; i<size; i++){
infile>>clock[i].hour>>colon>>clock[i].minute>>clock[i].M;
}
for (i=0; i<size; i++){
cout<<clock[i].hour << " "
<<colon << " "
<<clock[i].minute << " "
<<clock[i].M
<<endl;
}
}
Note that each line of your file contains three integral values, not two, and that a colon will stop reading in an integral value (formatted input for integrals will skip leading white spaces and even new line characters, but not "leading" colons). If you want to read an integral value that follows a colon, you'll need to skip the colon.
You could do this by reading in the colon into a variable of type char (and ignoring it afterwards). The code could look as follows:
int main()
{
int hour,minute,x;
char colon;
stringstream s { "15:43 10\n16:48 20\n" };
while (s >> hour >> colon >> minute >> x) {
cout << "H:M=" << hour << ":" << minute << "; times=" << x << std::endl;
}
}
Output:
H:M=15:43; times=10
H:M=16:48; times=20
Related
How can I get specified number?
For example, the number that a user enters is 4568962358.
I want to save the "96" in a variable; how do I get just the 96 or 23?
This is the real problem:
An important shipping company is making the annual inventory of the goods they have in their warehouse, for which they have implemented a system of codes that are assigned to each good that is in said warehouse. This code, which consists of 16 digits, contains the following information: unique number, indicator of whether it is fragile or not, place of origin of the property and expiration date of the property.
The assigned code structure is UUUFPPPPDDMMAAAA where:
UUU: unique number of the good.
F: digit that indicates if the good is fragile or not. If it is 0 it means that it is fragile.
PPPP: ASCII codes of the two letters that identify the country of origin of the good.
DD: Good's due date.
MM: Month of the expiration of the good.
AAAA: Year of expiration of the good.
You are asked to create a C ++ program that receives the assigned code as data and then prints the following data as shown in the example.
Enter the code: 1120677212042015
Then the program must print: Unique number: 112.
Fragile(N: No; S: Yes): S .
Country of origin: CH .
Day, month and year of expiration: 04-12-2015 .
Well it is outdated to date (N : No; S: Yes): S.
In the solution of the problem you will not be able to make use of selective structures.
Version 1
#include "stdafx.h"
#include "conio.h"
#include "iostream"
using namespace System;
using namespace std;
int main()
{
int code;
int UUU;
int F;
int PP1,PP2;
int DD;
int MM;
int AAAA;
cout << "Enter the code: "; cin >> code; // 1120677212042015
// ...code needed here
UUU = int(code / 10000000000000);
cout << "\nRESULTS" << endl;
cout << "\nUnique number: " << UUU << endl; // prints 112
_getch();
return 0;
}
Version 2
I'm not able to do the next parts; please help!
#include "stdafx.h"
#include "conio.h"
#include "iostream"
using namespace System;
using namespace std;
int main()
{
double code;
double UUU;
double F;
double PP1,PP2;
double DD;
double MM;
double AAAA;
cout << "Enter the code: "; cin >> code; // 1120677212042015
UUU = int(code / 10000000000000);
PP1 = int(code / 10000000000) % 100;
PP2 = int(code / 100000000) % 100;
DD = int(code / 1000000) % 100;
cout << "\nRESULTS" << endl;
cout << "\nUnique number: " << UUU << endl; // 112
cout << "Country of origin: " << (char)PP1 << (char)PP2<<endl; //CH
cout << "Day, Month and Year of expiration: " << DD; // 12/../....
_getch();
return 0;
}
One way is using substr concept.
For example, if user enters input as 1120677212042015, then read it into a string and divide the string into sub-strings according to the format UUUFPPPPDDMMAAAA .
std::string sUserCode = "1120677212042015";
std::string sUniqueNumber = str.substr (0,3);
int iUniqueNumber = stoi(sUniqueNumber);
This is one of those occasions where the C library comes in handy :
int UUU, F, PP1, PP2, DD, MM, AAAA;
cout << "Enter the code: "; // 1120677212042015
scanf("%3d %1d %2d %2d %2d %2d %4d", &UUU, &F, &PP1, &PP2, &DD, &MM, &AAAA);
This captures all the values. The spaces in the format string are not necessary, but makes it easier to read.
u can use % and / to do this
4568962358 / 10000 = 456896 %100 = 96
or you can put it in a array and show the part of integer that u want.
int n = 4568962358;
int a[10];
for (int i = 0; i<10 ; i++)
{
a[i] = n%10;
n /= 10;
}
search your question in stack over flow.
these questions have good answers.
I have this problem where I must read data from n books: title, author, price( the pret variable), the number of copies (the nr variable). The variable val represents the "value" of the book, which is price* number of copies.
We only use arrays in school, not vectors of strings therefore all of our problems have fixed-sized strings. Example of the file we are reading from:
3
Ion
Liviu Rebreanu
100
10
Mara
Ioan Slavici
50
3
Poezii
Mihai Eminescu
60
20
I need to print out the data read for every book and also the "value" of it. And I also need to print the data of the book with the highest value afterwards. I'm working in Code::Blocks 13.12 because it'a school assignment. I have no idea why, but it reads only the data for my first book. So therefore it prints a lot of nonsense after that reading. What's wrong with it?
#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;
struct carte
{
char t[50], a[50];
int pret, nr, val;
} v[100];
int main()
{
int n, i, maxx=0, x, j;
ifstream fin ("carte.txt");
fin>>n;
fin.get();
for (i=1; i<=n; i++)
{
fin.get(v[i].t, 50); cout<<v[i].t<<" ";
fin.get();
fin.get(v[i].a, 50);
fin.get(); cout<<v[i].a<<" ";
fin>>v[i].pret>>v[i].nr; cout<<v[i].pret<<" "<<v[i].nr<<endl;
v[i].val=v[i].pret*v[i].nr;
if(v[i].val>maxx)
{
maxx=v[i].val;
x=i;
}
}
for(i=1; i<=n; i++)
{
cout<<v[i].t<<" "<<v[i].a<<" "<<v[i].pret;
cout<<" "<<v[i].nr<<" "<<v[i].val<<endl;
}
cout<<v[x].t<<endl;
return 0;
}
Before you compute the total number of copies (pret * nr) you should make an extra call to fin.get() to deal with what I think is a carriage return. I noticed this after reformatting the code a bit to show the output in terms of a line of comma separated values, or csv.
#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;
struct carte
{
char t[50], a[50];
int pret, nr, val;
} v[100];
int main()
{
int n, i, maxx=0, x, j;
ifstream fin ("carte.txt");
fin>>n;
fin.get();
cout << "Read: " << n << endl;
cout << "Title, Author, Price, Copies" << endl;
for (i=1; i<=n; i++) {
fin.get(v[i].t, 50); cout<<v[i].t<<",";
fin.get();
fin.get(v[i].a, 50);
fin.get(); cout<<v[i].a<<",";
fin>>v[i].pret>>v[i].nr; cout<<v[i].pret<<","<<v[i].nr<<endl;
fin.get();
v[i].val=v[i].pret*v[i].nr;
if(v[i].val>maxx) {
maxx=v[i].val;
x=i;
}
}
cout << "Output: " << endl;
for(i=1; i<=n; i++) {
cout<<v[i].t<<" "<<v[i].a<<" "<<v[i].pret;
cout<<" "<<v[i].nr<<" "<<v[i].val<<endl;
}
cout<<v[x].t<<endl;
return 0;
}
Read: 3
Title, Author, Price, Copies
Ion,Liviu Rebreanu,100,10
Mara,Ioan Slavici,50,3
Poezii,Mihai Eminescu,60,20
Output:
Ion Liviu Rebreanu 100 10 1000
Mara Ioan Slavici 50 3 150
Poezii Mihai Eminescu 60 20 1200
Poezii
An interactive C++ program whose input is a series of 12 temperatures from the user. It should write out on file tempdata.dat each temperature as well as the difference between the current temperature and the one preceding it. The difference is not output for the first temperature that is input. At the end of the program, the average temperature should be displayed for the user via cout.
Here is What I have so far:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int counter = 0;
int previousTemp;
ofstream file; //declares a file of type ofstream
file.open("temperaturefile.txt"); //opens the file with open method
int temperature = 0;
int difference = 0;
cout << "Enter 12 temperatutes" << endl;
file << temperature;
while (counter < 12)
{
cin >> temperature;
counter++;
// difference = temperature-previousTemp;
// cout << difference << endl;
//
}
}
You have it commented in your code? I don't understand
difference = temperature-previousTemp;
You can keep track of previousTemp at the end of your loop.
After everything in the loop put
previousTemp=temperature;
I'm new to c++ programming and i tried to use struct arrays ( dunno if I say it in the way you understand ). Program does read the variables from the text file but it reads it for 2 times, prob It's not a huge problem as the variables are not changed but just in case
struct menesine_temperatura
{
int diena[10];
int rytas[10];
int pietus[10];
int vakaras[10];
};
int main()
{
menesine_temperatura mt;
int n;
ifstream failas("duomenys.txt");
while(!failas.eof())
{
failas >> n;
cout << n << endl;
for(int i = 0; i < n; i++)
{
failas >> mt.diena[i] >> mt.rytas[i] >> mt.pietus[i] >> mt.vakaras[i];
cout << mt.diena[i] << mt.rytas[i] << mt.pietus[i] << mt.vakaras[i] << endl;
}
}
failas.close();
return 0;
}
as I have mentioned before I' new to this stuff, so help would be appreciated.
With the code you have you, it will appear as if the last set of numbers are being read twice.
Here's a simple example that explains the logic error.
int main()
{
int n;
std::ifstream inf("data.txt");
while ( !inf.eof() )
{
inf >> n;
std::cout << n << std::endl;
}
}
and your input file consists of
10
20
In the first iteration of the loop, 10 is read into n and 10 is printed.
In the second iteration of the loop, 20 is read into n and 20 is printed.
At this point inf.eof() is still false. So you enter the loop the third time. At this time, nothing is read into n but the value of n still 20 and 20 is printed.
The logic error is that just because inf.eof() is false, you assume that there is still some data in the file that can be read.
A better way would be to use:
int main()
{
int n;
std::ifstream inf("data.txt");
while ( inf )
{
if ( inf >> n )
{
std::cout << n << std::endl;
}
}
}
I hope this gives you enough information on how to update your program.
First off, I'll mark this as a homework problem I've been stuck on for a week as I can't seem to figure out what I'm doing wrong and I'm hoping the wonderful people at SO can come to my rescue yet again (I've searched SO and other C++ sites for the past week but the solutions offered didn't correct the issue - however it is possible I may have been setting the loop incorrectly.
The assignment: given a text file numbers.txt (which contains 9,999 numbers ranging from 1 to 10,000 randomly sorted with one number missing from the consecutive list) the assignment is to use a void function in order to determine what the missing integer is.
What I've tried: My last attempt of this contains the following code:
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
void find_number();
int main()
{
...
find_number();
}
void find_number();
{
int sum = 0;
int sum1 = 0;
int num;
for (int i = 1; i <= 10000; i++)
sum += i;
cout << "The sum of all the numbers between 1 and 10,000 is: " << sum << endl;
ifstream numbers;
numbers.open("numbers.txt");
if (!numbers.good()) {
return;
cout << "Error! Unable to open file!";
}
if (numbers) {
numbers >> num;
sum1 += num;
}
numbers.close();
cout << "The sum of all the numbers contained in the text file \"numbers.txt\" is: " << sum1 << endl;
cout << "By subtracting the sum of the text file from the sum of 1 to 10,000 the consecutive number missing from the text file is: " << sum - sum1 << endl;
}
What am I doing wrong? Thank you for any assistance.
There are at least two mistakes:
The return statement is executed before the diagnostic output
if (!numbers.good()) {
return;
cout << "Error! Unable to open file!";
}
The following lines will execute once instead of reading the whole file:
if (numbers) {
numbers >> num;
sum1 += num;
}
You can improve your code with the following suggestions:
Extract a number and check the stream status at the same time:
while(numbers >> num) sum1 += num;
You don't need to close the file stream it will do it automatically in its destructor.
You can open the file at the file stream initialization time:
ifstream numbers("numbers.txt");
Hint: you are not reading the whole file