picking up random numbers from a text file in c++ - visual-c++

If a text file contains numbers in 100 rows * 100 columns (for example). Now i want my program to pick up ,for example , one number from 60th row and 97th column and then assign this value to a variable and perform some calculation with this variable. So i want to pick up some random numbers from a text file which contains a lot of numbers. how can i do that??
I made a code for practice but its giving some error.
the text file contains 6 different digits in 2 rows and 3 columns
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
int data[6],i=0;
ifstream myfile;
myfile.open ("a.txt");
while (i<<6)
{
myfile>>data[i];
i=i+1;
}
myfile.close();
cout<<data[0]<<"\t"<<data[1]<<"\t"<<data[2]<<"\t"<<data[3]<<"\t"<<data[4]<<"\t" <<data[5]<<"\n";
system("pause");
return 0;
}

while (i < 6)
{
myfile>>data[i];
i=i+1;
}

Related

Function returns Initials of first and last names

I need help solving this problem in my mind so if anoyne had a similar problem it would help me.
The problem is: Create a function that forms and returns a string containing that person's initials in the form "P.I" based on the person's last name and first name (which are sent as separate strings) (P is the first letter of the last name and I is the first letter of the first name).
I tried this:
#include <stdio.h>
#include <string.h>
char function(char *s1,char *s2)
{
char pom[100];
pom[0]=s1[0];
pom[1]='.';
pom[2]=s2[0];
pom[100]=pom[0]+pom[1]+pom[2];
return pom;
}
int main()
{
char s1[100];
char s2[100];
scanf("%s",s1);
scanf("%s",s2);
char pom[100];
pom[100]=function(s1,s2);
printf("%s",pom);
return 0;
}
But this is not working properly.
Thanks in advance !
Best regards !

I'm need to count every word, line , and character of a given string

as you can see i'm trying to get the word count, character count and line count but the below code is not working.
#include<iostream>
using namespace std;
int main()
{
char ch;
int wc=1, lc=1, cc=0;
while((ch=cin.get())!='*')
{
cc++;
if(ch==' ')
{
wc++;
}
else if(ch=='\n')
{
wc++;
lc++;
}
}
cout<<"\n the number of character=="<<cc;
cout<<"\n the number of words=="<<wc;
cout<<"\n the number of lines=="<<lc;
return 0;
}
I entered your code and compiled it with g++. It is working without any problems. Can you post the error you get or did it compile ?
Maybe your visual c++ compiler is not working right. The code itself should work.
Edit: Below a different version of the above code, where no text input is threaded as zero words and EOF is also a break condition of the loop.
EOF depends on your system, on Windows it is Control + z, on Linux it might be Control + d.
The input text might have multiple spaces between words. Punctuation characters and digits (0-9) are threaded as word delimiters as good as possible. Underscore, backticks, tildes and apostrophe like in "don't" are handled as part of a word.
Curly brackets are handled as part of a word to keep the code simple but normal brackets are delimiters.
#include <iostream>
using namespace std;
int main()
{
int ch, wc=0, lc=1, cc=0, old=0;
cout<<"Enter your text, exit with '*':\n";
while ((ch=cin.get())!='*' && ch!=EOF)
{
cc++;
if (old<='?' && old!='\'')
wc += !(ch<='?' && ch!='\'');
lc += ((old=ch)=='\n');
}
cout<<"\nthe number of character=="<<cc
<<"\nthe number of words=="<<wc
<<"\nthe number of lines=="<<lc<<"\n";
return 0;
}

.txt to binary c++, issue with int

I have a text file that I am converting to binary. Its a 7 digit no. followed by a name and then repeat for however many names is listed.
1234567 First Last
7654321 First Last
Because its a 7 digit int, I am having trouble outputting it to the binary using this method with the int struct. It gives me an awfully large .DAT (binary) file whenever I write to it even with say just 3 names. Is there a better way of outputting it so my binary .dat files look about 200kb and doesn't end up in the 20mb+ range?
const int MAX = 50;
struct StudentRegistration{
int studentID;
char name[MAX];
};
fstream afile;
ifstream infile;
afile.open (fileName2, ios::out | ios::binary);
infile.open (fileName1);
while (infile >> s.studentID)
{
infile.get(space);
infile.getline(&s.name, MAX);
afile.seekp((s.studentID-1)*sizeof(StudentRegistration), ios::beg);
afile.write(reinterpret_cast <const char *>(&s), sizeof(s));
}
afile.close();
infile.close();
I removed the seekp line and it seems to do what I want to it for now.

Loop to keep adding spaces in string?

I have the following code:
sHexPic = string_to_hex(sPic);
sHexPic.insert(sHexPic.begin() + 2,' ');
sHexPic.insert(2," ");
I would like to know how I can put this into a counted loop and add a space after every 2nd character. So far all this does is make this string "35498700" into "35 498700", which in the end I want the final result to be something like "35 49 87 00".
I assume you would have to get the length of the string and the amount of characters in it.
I am trying to achieve this in c++/cli.
Thanks.
Here's how it would be done in C++, using a string :) (I'm using C libraries cuz I'm more familiar with C)
#include <stdio.h>
#include <string>
using namespace std;
int main()
(
string X;
int i;
int y;
X = 35498700;
y= X.size();
for(i=2;i<y;i+=2)
{
X.insert(i," ");
y=x.size(); //To update size of x
i++; //To skip the inserted space
}
printf("%s",X);
return 0;
}
Have fun :)
That would "probably" work. If it didn't then please mention so :)

Read line by line from text file c++

I have a file with random data for accounts.
The data in the file:
5
2871 2.19 8
1234 95.04 23
3341 0.00 10
3221 -1.08 21
7462 404.14 4
3425 4784.00 200
3701 99.50
JUNK SHOULD NEVER GET HERE
3333
The first number 5 will always be the number of accounts that need to be processed.
I want to be able to read that number and set it as the number of accounts.
So my question is how can I read the file and read line by line and set the first number to the number of accounts that need to be processed.
Code so far:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
double NumberOfAccounts;
ifstream File("test.dat");
string line;
if(File)
{
while(getline(File,line))
{
NumberOfAccounts=line[0];
}
File.close();
}
cout<<NumberOfAccounts;
system("pause");
return 0;
}
Right now it just prints out 51.
Any tips/help would be appreciated.
Two things. One, you're getting stuck in a while loop (while there's a line left, read it in and re-assign the number of accounts) until the end of the file. Secondly, ASCII numbers do not correspond to actual numbers, so the character "0" is actually the number 48. You're getting 51 as the program reads the last line, finds the "3" character, assigns that to an integer (which is now 51), then outputs it.
NumberOfAccpounts is double, you are assigning the first character of line...
I assume you menat the first line in the file.
My C++ is crap so
pseudocode
if(File)
{
if getLine(File, line)
{
NumberOfAccounts =atof(line);
}
File.close();
}
cout<<NumberOfAccounts;
system("pause");
return 0;
atof is one way to convert a string to a double. You don't need to read the entire file to get the first line.

Resources