Question

Convert this java code to C++. Also i have given the file format below for reference....

Convert this java code to C++. Also i have given the file format below for reference. The file has a name and marks separated by a white space. Want to get the name and parse numbers (marks) into a vector given as: vector<int> marks

Paul 24 56 73 80 90



public StudentEntry(String line) {
int nameEnd = line.indexOf(‘ ‘);
name = line.substring(0, nameEnd);
line=line.substring(nameEnd +1);

String[ ] splitLine =line.split(“\\s”);

for (int i=0; i<size; i++) {
marks[i] = Integer.parseInt(splitLine[i]);
}
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <string.h>
#include <iostream>
#include <vector>

using namespace std;
  
int main()
{
char str[] = "Paul 24 56 73 80 90 ";
vector<int> marks;

// Returns first token
char *token = strtok(str, " ");
string name=token;
cout<<"Name : "<<name;
token = strtok(NULL, " ");
// Keep printing tokens while one of the
// delimiters present in str[].
while (token != NULL)
{
int n = stoi(token);
marks.push_back(n);
token = strtok(NULL, " ");
}
cout<<"Marks : \n";
for(int i=0;i<marks.size();i++)
cout<<marks[i]<<" ";
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Convert this java code to C++. Also i have given the file format below for reference....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am given an input file, P1input.txt and I have to write code to find the...

    I am given an input file, P1input.txt and I have to write code to find the min and max, as well as prime and perfect numbers from the input file. P1input.txt contains a hundred integers. Why doesn't my code compile properly to show me all the numbers? It just stops and displays usage: C:\> java Project1 P1input.txt 1 30 import java.io.*; // BufferedReader import java.util.*; // Scanner to read from a text file public class Project1 {    public static...

  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

  • Can Anyone help me to convert Below code to C++! Thanks For example, in C++, the...

    Can Anyone help me to convert Below code to C++! Thanks For example, in C++, the function headers would be the following: class MaxHeap { vector<int> data; public: MaxHeap() { // ... } int size() { // ... } int maxLookup() { // ... } void extractMax() { // ... } void insert(int data) { // ... } void remove(int index) { // ... } }; ======================== import java.util.Arrays; import java.util.Scanner; public class MaxHeap { Integer[] a; int size; //...

  • Convert Code from Java to C++ Convert : import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class...

    Convert Code from Java to C++ Convert : import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Template { public static void main(String args[]) { Scanner sc1 = new Scanner(System.in); //Standard input data String[] line1 = sc1.nextLine().split(","); //getting input and spliting on basis of "," String[] line2 = sc1.nextLine().split(" "); //getting input and spiliting on basis of " "    Map<String, String> mapping = new HashMap<String, String>(); for(int i=0;i<line1.length;i++) { mapping.put(line1[i].split("=")[0], line1[i].split("=")[1]); //string in map where in [] as key and...

  • Public class File {    public String base; //for example, "log" in "log.txt"  &nbs...

    public class File {    public String base; //for example, "log" in "log.txt"    public String extension; //for example, "txt" in "log.txt"    public int size; //in bytes    public int permissions; //explanation in toString public String getExtension() {        return extension;    } public void setExtension(String e) {           if(e == null || e.length() == 0)            extension = "txt";        else            extension = e;    } public class FileCollection {    private File[] files;    /**    * DO NOT MODIFY    * Loads collection from input file    * @param input: name...

  • Information About This Project             In the realm of database processing, a flat file is a...

    Information About This Project             In the realm of database processing, a flat file is a text file that holds a table of records.             Here is the data file that is used in this project. The data is converted to comma    separated values ( CSV ) to allow easy reading into an array.                         Table: Consultants ID LName Fee Specialty 101 Roberts 3500 Media 102 Peters 2700 Accounting 103 Paul 1600 Media 104 Michael 2300 Web Design...

  • DatabaseFilter Design a DatabaseFilter class that will manage the file 1/O (input/output) of an employee ArrayList...

    DatabaseFilter Design a DatabaseFilter class that will manage the file 1/O (input/output) of an employee ArrayList object. For a Database Filter class to actually store an ArrayList object, it must know the file to which the list must be read from or written to. Note: It is expected that you may have more methods within your class design than requested in the specifications described below. However, your class should support at a minimum the following data and operations: encapsulated data...

  • The following code uses a Scanner object to read a text file called dogYears.txt. Notice that...

    The following code uses a Scanner object to read a text file called dogYears.txt. Notice that each line of this file contains a dog's name followed by an age. The program then outputs this data to the console. The output looks like this: Tippy 2 Rex 7 Desdemona 5 1. Your task is to use the Scanner methods that will initialize the variables name1, name2, name3, age1, age2, age3 so that the execution of the three println statements below will...

  • Using Java, I am trying to read in a file with the format outlined below. "AARON,...

    Using Java, I am trying to read in a file with the format outlined below. "AARON, ELVIA J"   WATER RATE TAKER   WATER MGMNT   "$87,228.00" "AARON, JEFFERY M"   POLICE OFFICER   POLICE   "$75,372.00" "AARON, KARINA"   POLICE OFFICER   POLICE   "$75,372.00" "AARON, KIMBERLEI R"   CHIEF CONTRACT EXPEDITER   GENERAL SERVICES   "$80,916.00" "ABAD JR, VICENTE M"   CIVIL ENGINEER IV   WATER MGMNT   "$99,648.00" "ABARCA, ANABEL"   ASST TO THE ALDERMAN   CITY COUNCIL   "$70,764.00" "ABARCA, EMMANUEL"   GENERAL LABORER - DSS   STREETS & SAN   "$40,560.00" "ABBATACOLA, ROBERT J"   ELECTRICAL MECHANIC  ...

  • Implement the function hasDuplicates. Input will be given as a line containing a positive integer n,...

    Implement the function hasDuplicates. Input will be given as a line containing a positive integer n, followed by n rows, each containing a string. The output should be either the word true if there are any duplicates among these strings or false if there are not. Your code should work well on long lists of strings. Use the following set-up to write the code in JAVA import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { public static void main(String[] args) {...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT