write in one row

Hi
I'm writing a program to do the following :
it'll write the names, IDs, etc for people in a predefined txt file,
the problem is, I need to write some information in the same row, not in the next one, like:
John 123214
not:

John
123214

in my below code (The last "void printname" , " void printadd") , it'll do it but the second way( note that: its done through menus and sub-menus)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include<stdio.h>
#include<stdlib.h>
#include <fstream>
#include <string>
#include <iostream>
#include <iomanip>

/* Prototype */
int ShowMainMenu();
int ShowStudMenu();
int ShowStudSubMenu();
int ShowMarkMenu();
int ShowMarkSubMenu();
 
void PrintID();
void PrintSmark();
void PrintFmark();
 
void PrintName();
void PrintAdd();

using namespace std;


int main(void)
{
   int 
      nChoice = 0;
 
   do
   {
      nChoice = ShowMainMenu ();
      switch (nChoice)
      {
         case 1:
         {
            ShowStudMenu ();            
         }
         break;
 
         case 2: 
         {
            ShowMarkMenu ();
         }
         break;
 
         case 7: 
         {
            printf ("Bye Bye\n\n");
         }
         break;
      }
   }
   while (nChoice != 7);
   system ("Terminating Program");
   return 0;
} 
 
/* ************************************************** ******************* */
 
int ShowMainMenu ()
{
   int 
      nSelected = 0;
 
   do
   {
      printf ("This is a program to store students data and retrieve it back\n");
      printf ("You can choose a number of your choice for the desired task\n\n");
 
      printf ("(1) Add new students\n(2) Add student's marks\n(3) Print student info\n(4) Print the list of marks\n(5) Print the list of final marks\n(6)Print the the final results\n(7) Bye :)\n\n");
 
      printf ("Enter a number that corresponds to your choice > ");
      scanf ("%d", &nSelected);
      printf("\n");
 
      if (( nSelected < 1 ) || ( nSelected > 7))
      {
         printf("You have entered an invalid choice. Please try again\n\n\n");
      }
   }
   while (( nSelected < 1) || ( nSelected > 7));
 
   return nSelected;
}
 
/* ************************************************** ************************** */
int ShowStudMenu ()
{   
   int 
      nChoice = 0;   
   do
   {
      nChoice = ShowStudSubMenu();
      switch (nChoice)
      {
 
         case 1:
         {
            PrintName ();
         }
         break;
 
         case 2: 
         {
            PrintAdd ();
         }
         break;
 
         case 3: 
         {
            printf ("Bye Bye\n\n");
         }
         break;
      }
   }
   while (nChoice != 3);
 
   return nChoice;
}
 
/* ************************************************** ******************* */
int ShowStudSubMenu() 
{
   int 
      nSelected = 0;
 
   do
   {
 
      printf ("(1) Add Student's Name\n(2) Add student's address\n(3) Back\n\n");
 
      printf ("Enter a number that corresponds to your choice > ");
      scanf ("%d", &nSelected);
      printf("\n");
 
      if (( nSelected < 1 ) || ( nSelected > 3))
      {
         printf("You have entered an invalid choice. Please try again\n\n\n");
      }
   }
   while (( nSelected < 1) || ( nSelected > 3));
 
   return nSelected;
}
 
/* ************************************************** ************************** */
int ShowMarkMenu ()
{
   int 
      nChoice = 0;
 
   do
   {
      nChoice = ShowMarkSubMenu ();
      switch (nChoice)
      {
         case 1:
         { 
            PrintID ();
         }
         break;
 
         case 2:
         { 
            PrintSmark ();
         }
         break;
 
         case 3:          
         {             
            PrintFmark ();
         }
         break;
 
         case 4:
         { 
            printf ("Bye Bye\n\n");
         }
         break;
      }
   }
   while (nChoice != 4);
 
   return nChoice;
}
 
/* ************************************************** ******************* */
int ShowMarkSubMenu ()
{
   int 
      nSelected = 0;
 
   do
   {
 
      printf ("(1) Add student's number ID\n(2) Add student's semester marks\n(3) Add student's final marks\n(4) Quit\n\n");
 
      printf ("Enter a number that corresponds to your choice > ");
      scanf ("%d", &nSelected);
      printf("\n");
 
      if (( nSelected < 1 ) || ( nSelected > 4))
      {
         printf("You have entered an invalid choice. Please try again\n\n\n");
      }
   }
   while (( nSelected < 1) || ( nSelected > 4));
 
   return nSelected;
}
 
/* ************************************************** ************************** */
void PrintName ()
{
    std::ofstream outfile;
    std::setw; 
    char name[18]; 
    char address[18]; 
    outfile.open("Student.txt", std::ios_base::app);
    cout<<"Enter the student Name: ";
    cin>>name;
    cin.ignore();
    outfile<<name<<setw(2)<<address<<endl;
    outfile.close();
    cout<<"Writing Completed."<<endl;
}
 
/* ************************************************** ************************** */
void PrintAdd ()
{
    std::ofstream outfile;
    std::setw; 
    char name[18];
    char address[18]; 
    outfile.open("Student.txt", std::ios_base::app);
    cout<<"Enter the student address: ";
    cin>>address;
    cin.ignore();
    outfile<<name<<setw(2)<<address<<endl;
    outfile.close();
    cout<<"Writing Completed."<<endl;
}

Last edited on
WOW!
really? no one have a clue !
Or maybe it's because your code is broken in ways that extend beyond writing on separate lines. You're including C headers on lines 1 and 2, line 55 almost certainly is wrong, you're mixing C and C++ I/O (cout vs printf and cin vs scanf), you're using C-style strings on lines 218-219 and 234-235, one of those strings in each function is not initialized.

However, as for the issue you mention, you've split writing one line into two functions which may not be called in the correct order. Each function writes a line. You'll likely either need to assure that the functions are called in the write order, or have the actual write happen outside of either function.

-Albatross
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <fstream>

void print()
{
    std::string name ;
    std::cout << "name? " ;
    std::getline( std::cin, name ) ;

    std::string address ;
    std::cout << "address? " ;
    std::getline( std::cin, address ) ;

    std::ofstream file( "Student.txt", std::ios_base::app ) ;
    file << name << " @ " << address << '\n' ;
}

http://coliru.stacked-crooked.com/a/2c87a3abac5fdfd7
Topic archived. No new replies allowed.